Skip to main content

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โ€‹

TriggerRollback?
Deployment failure (any hook fails)โœ… Automatic
CloudWatch Alarm threshold breachedโœ… Automatic (if configured)
Manual rollbackโœ… Manual via console/CLI
tip

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โ€‹

ConfigDescription
Deployment groupTarget instances (by EC2 tags, ASG, or ECS service)
Deployment configTraffic shifting strategy
Service roleIAM role for CodeDeploy
AlarmsCloudWatch alarms that trigger rollback
TriggersSNS 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%.


๐Ÿ”— Resourcesโ€‹