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:
- Deploy new version to green environment
- Run tests against green
- Switch load balancer to route all traffic to green
- Monitor for issues
- 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:
- Deploy new version alongside old version
- Route 5% of traffic to new version
- Monitor error rates, latency, and metrics
- Gradually increase: 5% -> 25% -> 50% -> 100%
- 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:
- Take one instance out of service
- Deploy new version to that instance
- Run health checks
- Return instance to service
- 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?
2. In a canary deployment, what happens after the initial small percentage of traffic is routed to the new version?
3. What is the purpose of lifecycle hooks in CodeDeploy?
Flashcards
Question
What is blue-green deployment?
Click to reveal answer
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?
Click to reveal answer
Answer
Gradually shifts traffic from old to new version, starting small and increasing while monitoring for issues.
Question
What is rolling deployment?
Click to reveal answer
Answer
Replaces instances one at a time or in batches, maintaining availability during the transition.
Question
What is CodeDeploy?
Click to reveal answer
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.