Skip to content
intermediate Phase 11 · CI/CD Pipelines

Deployment Strategies

Implement blue-green, canary, and rolling deployments with CodeDeploy. Configure alarms, rollback policies, and traffic shifting.

55m
0 problems
Topic Progress 0%

Blue-Green Deployment

Blue-green deployment maintains two identical environments: blue (current) and green (new). Traffic switches from blue to green in one step.

Process:

  1. Deploy new version to green environment
  2. Run tests against green
  3. Switch load balancer to route all traffic to green
  4. Monitor for issues
  5. If issues occur, switch back to blue

Implementation with ALB:

# CloudFormation: create green target group
GreenTargetGroup:
  Type: AWS::ElasticLoadBalancingV2::TargetGroup
  Properties:
    HealthCheckPath: /health
    Port: 80
    VpcId: !Ref VPC

# Switch traffic by updating listener rule
ListenerRule:
  Type: AWS::ElasticLoadBalancingV2::ListenerRule
  Properties:
    Actions:
      - Type: forward
        TargetGroupArn: !Ref GreenTargetGroup

Advantages: instant rollback (switch back to blue), zero downtime, full testing of new version before traffic switch. Disadvantages: requires double infrastructure during deployment, more complex setup.

Canary Deployment

Canary deployment gradually shifts traffic from old to new version. Start with a small percentage (1-5%), monitor for issues, then increase gradually.

Process:

  1. Deploy new version alongside old version
  2. Route 5% of traffic to new version
  3. Monitor error rates, latency, and metrics
  4. Gradually increase: 5% -> 25% -> 50% -> 100%
  5. Roll back if any metric degrades

Implementation with CodeDeploy:

DeploymentConfig:
  Type: AWS::CodeDeploy::DeploymentConfig
  Properties:
    DeploymentConfigName: Canary10Percent5Minutes
    MinimumHealthyHosts:
      Type: FLEET_PERCENT
      Value: 90
    TrafficRoutingConfig:
      Type: CanarySwitchTimeBased
      TimeBasedCanary:
        CanaryPercentage: 10
        CanaryInterval: 5

Canary with Lambda alias traffic shifting:

TrafficShiftingConfig:
  Type: Canary10Percent5Minutes
  LambdaFunctionVerifyStepNotifications: true

Advantages: reduced risk with gradual exposure, real-time monitoring of production traffic. Disadvantages: longer deployment time, requires sophisticated monitoring.

Rolling Deployment

Rolling deployment replaces instances one at a time or in batches. At any point, a mix of old and new versions serves traffic.

Process:

  1. Take one instance out of service
  2. Deploy new version to that instance
  3. Run health checks
  4. Return instance to service
  5. Repeat for next instance

CodeDeploy rolling deployment with ECS:

DeploymentConfig:
  Type: AWS::CodeDeploy::DeploymentConfig
  Properties:
    DeploymentConfigName: CodeDeployDefault.ECSCanary10Percent5Minutes
    ComputePlatform: ECS
    TrafficRoutingConfig:
      Type: CanarySwitchTimeBased
      TimeBasedCanary:
        CanaryPercentage: 10
        CanaryInterval: 5

Instance replacement strategy: CodeDeploy can use linear, canary, or all-at-once deployment configurations. For EC2, it terminates old instances and launches new ones. For ECS, it updates the task definition and shifts traffic.

Advantages: simple, no extra infrastructure needed, continuous availability. Disadvantages: slower than blue-green, mixed versions during deployment, harder to rollback quickly.

CodeDeploy: Application Config, Deployment Groups, Hooks

AWS CodeDeploy manages deployments across EC2, ECS, Lambda, and on-premises.

Application: The compute platform (EC2/On-Premises, ECS, Lambda).

Deployment group: Targets for deployment (EC2 instances by tag, ECS services, Lambda aliases). Each group has its own deployment configuration and hooks.

Deployment configuration: Defines deployment behavior (canary, linear, all-at-once).

Lifecycle hooks run scripts at specific points:

  • BeforeInstall: pre-deployment setup
  • AfterInstall: post-install configuration
  • BeforeApplicationStart: start dependencies
  • AfterApplicationStart: verify application started
  • BeforeAllowTraffic: pre-traffic validation
  • AfterAllowTraffic: post-traffic validation
DeploymentGroup:
  Type: AWS::CodeDeploy::DeploymentGroup
  Properties:
    ApplicationName: !Ref CodeDeployApp
    DeploymentConfigName: CodeDeployDefault.LinearAllAtOnceIn10Minutes
    AutoRollbackConfiguration:
      Enabled: true
      Events: [DEPLOYMENT_FAILURE, DEPLOYMENT_STOP_ON_REQUEST]
    AlarmConfiguration:
      Alarms:
        - !Ref ErrorAlarm

Auto rollback triggers automatically revert if alarms fire during deployment. Configure CloudWatch alarms for error rates, latency, or custom metrics.

Quiz

1. What is the main advantage of blue-green deployment?

Question 1 options

2. In a canary deployment, what happens after the initial small percentage of traffic is routed to the new version?

Question 2 options

3. What is the purpose of lifecycle hooks in CodeDeploy?

Question 3 options

Flashcards

Question

What is blue-green deployment?

Answer

Maintains two identical environments and switches traffic from blue (current) to green (new) in one step, enabling instant rollback.

Question

What is canary deployment?

Answer

Gradually shifts traffic from old to new version, starting small and increasing while monitoring for issues.

Question

What is rolling deployment?

Answer

Replaces instances one at a time or in batches, maintaining availability during the transition.

Question

What is CodeDeploy?

Answer

AWS service that automates deployments across EC2, ECS, Lambda with canary, linear, and all-at-once strategies.

Revision Notes

Key Takeaways

  • 1. Blue-green: instant rollback, requires double infrastructure
  • 2. Canary: gradual traffic shift, reduced risk, needs monitoring
  • 3. Rolling: simple, mixed versions during deployment
  • 4. CodeDeploy manages lifecycle hooks, auto rollback, and alarm integration

Interview Tips

  • Compare blue-green, canary, and rolling deployments with trade-offs
  • Explain how you would implement canary deployment for a Lambda function
  • Describe CodeDeploy lifecycle hooks and when you would use each
  • Discuss rollback strategies for each deployment type

Cheat Sheet

Blue-green: two environments, switch traffic, instant rollback. Canary: gradual traffic shift, monitor metrics. Rolling: replace instances in batches. CodeDeploy: application -> deployment group -> config. Hooks: BeforeInstall, AfterInstall, BeforeAllowTraffic, AfterAllowTraffic.