AWS Certified DevOps Engineer – Professional
CodeDeploy AppSpec lifecycle hooks: order and ApplicationStop
In an in-place EC2 deployment, CodeDeploy runs ApplicationStop, BeforeInstall, AfterInstall, ApplicationStart and ValidateService in that order. The CodeDeploy User Guide says ApplicationStop uses the scripts of the last successfully deployed revision, so it does not run on the first deployment to an instance. A load balancer adds six traffic hooks.
What order do the hooks run in?
Without a load balancer in the deployment group, the order is ApplicationStop, DownloadBundle, BeforeInstall, Install, AfterInstall, ApplicationStart, ValidateService. DownloadBundle and Install are reserved for the CodeDeploy agent and cannot run scripts. What Install copies is set in the files section of the AppSpec file.
With a Classic Load Balancer, Application Load Balancer or Network Load Balancer in the deployment group, three hooks come first: BeforeBlockTraffic, BlockTraffic, AfterBlockTraffic. Then the same sequence runs from ApplicationStop to ValidateService, followed by BeforeAllowTraffic, AllowTraffic and AfterAllowTraffic. BlockTraffic and AllowTraffic are reserved for the agent.
BeforeBlockTraffic runs while the instance is still registered with the load balancer. AfterBlockTraffic runs once it is deregistered. BeforeAllowTraffic runs before the instance is registered again, and AfterAllowTraffic after.
Why does ApplicationStop run the old script?
ApplicationStop happens before the new revision is downloaded. The agent therefore takes the AppSpec file and scripts for this event from the previous successfully deployed revision. The same is true of BeforeBlockTraffic and AfterBlockTraffic. All other hooks use the AppSpec file of the revision being deployed.
Two results follow. A change to the stop script takes effect one deployment later than the revision that carries it. And on the first deployment to an instance there is no earlier AppSpec file, so ApplicationStop does not run at all. To stop a process during a first deployment, use a hook that runs from the new revision, such as BeforeInstall.
Timeouts and files that already exist
Each hook entry has a location, an optional timeout and an optional runas user. The default timeout is 3600 seconds, which is also the maximum for the scripts of one lifecycle event. If the scripts run longer, the deployment to that instance fails.
file_exists_behavior in the AppSpec file controls what happens when a target file exists but was not put there by the previous successful deployment. DISALLOW fails the deployment and is the default. OVERWRITE replaces the file with the revision's version. RETAIN keeps the file already on the instance. It is set once and applies to everything under files, and it takes precedence over the --file-exists-behavior CLI option.
Where the exam catches people
- Believing a new
ApplicationStopscript runs in the deployment that ships it: the agent runs the script from the previous successful revision. - Believing
ApplicationStopruns on the first deployment to an instance: there is no earlier AppSpec file, so the hook is skipped. - Believing the application should be stopped in
BeforeBlockTraffic: at that point the instance is still registered and receiving requests.ApplicationStopruns after traffic is blocked. - Believing scripts can be attached to
Install,DownloadBundle,BlockTrafficorAllowTraffic: these events are reserved for the CodeDeploy agent. - Believing CodeDeploy overwrites files it did not deploy: the default
file_exists_behaviorisDISALLOW, which fails the deployment.