AWS Certified Developer – Associate
CodeDeploy AppSpec lifecycle hooks: EC2, Lambda, ECS (DVA-C03)
According to the CodeDeploy User Guide, the AppSpec hooks section depends on the compute platform. EC2/On-Premises hooks run scripts on each instance, ending with ValidateService. Lambda has two hooks, BeforeAllowTraffic and AfterAllowTraffic. Amazon ECS has five, including AfterAllowTestTraffic. Lambda and ECS hooks are Lambda validation functions that report Succeeded or Failed.
EC2/On-Premises hooks run scripts
For EC2/On-Premises the AppSpec file must be YAML, named appspec.yml, and placed in the root of the revision. Otherwise deployments fail. Each hook entry has a script location, an optional timeout and an optional runas user. The default and maximum time for the scripts of one lifecycle event is 3600 seconds.
ApplicationStop occurs before the new revision is downloaded. It uses the AppSpec file and scripts from the last successfully deployed revision, and it does not run on the first deployment to an instance. BeforeInstall is for preinstall tasks such as decrypting files or taking a backup. AfterInstall is for configuring the application or changing file permissions. ApplicationStart restarts services that were stopped. ValidateService is the last deployment lifecycle event and is used to verify that the deployment completed successfully.
DownloadBundle, Install, BlockTraffic and AllowTraffic are reserved for the CodeDeploy agent and cannot run scripts. With a load balancer, BeforeBlockTraffic and AfterBlockTraffic run around deregistration, and BeforeAllowTraffic and AfterAllowTraffic run around registration.
Example: a smoke test that must run after the application process has restarted goes on ValidateService, not on AfterInstall.
Lambda and ECS hooks are validation functions
On the Lambda and Amazon ECS platforms each hook names one Lambda function, and each hook runs once per deployment. The function calls PutLifecycleEventHookExecutionStatus with the deployment ID, the hook execution ID and a status of Succeeded or Failed. If CodeDeploy is not notified within one hour, the deployment is considered failed.
A Lambda deployment has two hooks. BeforeAllowTraffic runs before traffic is shifted to the deployed function version. AfterAllowTraffic runs after all traffic is shifted.
An Amazon ECS deployment has five hooks: BeforeInstall, AfterInstall, AfterAllowTestTraffic, BeforeAllowTraffic and AfterAllowTraffic. BeforeInstall runs before the replacement task set is created, and a rollback is not possible at that point. AfterAllowTestTraffic runs after the test listener serves traffic to the replacement task set. BeforeAllowTraffic runs after the second target group is associated with the replacement task set but before production traffic shifts. The result of every ECS hook except BeforeInstall can trigger a rollback.
What does a deployment configuration control?
A deployment configuration is the set of rules and success and failure conditions for a deployment. On EC2/On-Premises it sets how many instances must stay available. CodeDeployDefault.OneAtATime is the default. CodeDeployDefault.HalfAtATime deploys to up to half of the instances at a time. CodeDeployDefault.AllAtOnce succeeds if the revision is deployed to at least one instance.
With OneAtATime the deployment fails as soon as any instance but the last one fails. If only the last instance fails, the overall deployment still shows as succeeded.
On Lambda and Amazon ECS the configuration sets how traffic shifts: canary, linear or all at once. CodeDeployDefault.LambdaCanary10Percent5Minutes shifts 10 percent first and the remaining 90 percent five minutes later. CodeDeployDefault.LambdaLinear10PercentEvery1Minute shifts 10 percent every minute. With a Network Load Balancer, only CodeDeployDefault.ECSAllAtOnce is supported among the predefined ECS configurations.
Where the exam catches people
- Believing
ValidateServiceexists for Lambda deployments: Lambda has onlyBeforeAllowTrafficandAfterAllowTraffic, and they run Lambda functions, not scripts on an agent. - Believing
BeforeAllowTrafficis the first ECS hook that can test the new tasks:AfterAllowTestTrafficruns earlier, once the test listener serves the replacement task set. - Believing a fixed
ApplicationStopscript in the new revision runs on the next deployment:ApplicationStopuses the scripts from the last successfully deployed revision. - Believing a canary configuration and a linear configuration are the same: canary shifts in two increments, and linear shifts equal increments at a fixed interval.
- Believing a
Succeededstatus underOneAtATimemeans every instance was updated: a failure on the last instance alone still reports success.