AWS CodeDeploy
Deployment Lifecycle Event Hooksโ
EC2/On-Premises Hook Orderโ
ApplicationStop
โ
DownloadBundle
โ
BeforeInstall
โ
Install
โ
AfterInstall
โ
ApplicationStart
โ
ValidateService โ Run health checks here
Lambda Hook Orderโ
BeforeAllowTraffic โ Run pre-traffic validation Lambda
โ
AllowTraffic โ Traffic shifted to new version
โ
AfterAllowTraffic โ Run post-traffic validation Lambda
Rollback Behaviorโ
| Trigger | Rollback? |
|---|---|
| Deployment failure (any hook fails) | โ Automatic |
| CloudWatch Alarm threshold breached | โ Automatic (if configured) |
| Manual rollback | โ Manual via console/CLI |
CodeDeploy "rollback" redeploys the previous revision, it doesn't reverse the deployment โ it deploys the old version again.
appspec.yml โ Lambdaโ
version: 0.0
Resources:
- MyLambdaFunction:
Type: AWS::Lambda::Function
Properties:
Name: "OrderProcessor"
Alias: "live"
CurrentVersion: !Sub "${LambdaVersion1}"
TargetVersion: !Sub "${LambdaVersion2}"
Hooks:
- BeforeAllowTraffic: !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:PreTrafficHook"
- AfterAllowTraffic: !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:PostTrafficHook"
Deployment Groupโ
| Config | Description |
|---|---|
| Deployment group | Target instances (by EC2 tags, ASG, or ECS service) |
| Deployment config | Traffic shifting strategy |
| Service role | IAM role for CodeDeploy |
| Alarms | CloudWatch alarms that trigger rollback |
| Triggers | SNS on deployment events |
๐งช Practice Questionsโ
Q1. A CodeDeploy deployment fails during the ValidateService hook. What happens next?
A) Deployment is marked failed but no rollback
B) CodeDeploy rolls back to the previous version
C) CodeDeploy retries the hook 3 times
D) CodeDeploy skips the hook and continues
โ Answer & Explanation
B โ Any hook failure causes CodeDeploy to automatically roll back to the last successful deployment.
Q2. Which CodeDeploy deployment configuration sends 10% of traffic to a new Lambda version, waits 5 minutes monitoring CloudWatch alarms, then shifts 100%?
A) LambdaLinear10PercentEvery1Minute
B) LambdaAllAtOnce
C) LambdaCanary10Percent5Minutes
D) LambdaBlueGreen
โ Answer & Explanation
C โ Canary10Percent5Minutes = shift 10% traffic โ monitor for 5 minutes โ if healthy, shift remaining 90%.