Certopolis

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 ApplicationStop script runs in the deployment that ships it: the agent runs the script from the previous successful revision.
  • Believing ApplicationStop runs 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. ApplicationStop runs after traffic is blocked.
  • Believing scripts can be attached to Install, DownloadBundle, BlockTraffic or AllowTraffic: these events are reserved for the CodeDeploy agent.
  • Believing CodeDeploy overwrites files it did not deploy: the default file_exists_behavior is DISALLOW, which fails the deployment.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A company releases an application with AWS CodeDeploy in-place deployments to Amazon EC2 instances, and the deployment group names an Application Load Balancer target group. An external session router decides which instance receives each user's next request. In future releases, a script must tell the router to stop placing new sessions on an instance while that instance is still registered and serving, before the load balancer stops sending it requests. The application must be stopped only after the instance has stopped receiving traffic. Which solution will meet these requirements?

    Right. In the game that’s Renown for your SDLC Automation building.

    Not that one: A is right. In the game your SDLC Automation building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    So BeforeBlockTraffic is the one scriptable point where the instance is still registered and serving, which is where the router must be told, and ApplicationStop already runs after traffic has stopped, which is right for stopping the application. Like ApplicationStop, BeforeBlockTraffic scripts are read from the last successful revision, so the hook takes effect from the release after the one that first carries it.

    Written from docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html

  2. A company's pipeline releases a job-processing application to Amazon EC2 instances with AWS CodeDeploy in-place deployments, with no load balancer in the deployment group. Release 41, planned for Friday, adds a graceful drain: before the running version stops on an instance, it must finish its in-flight jobs. A developer has added the drain step to the ApplicationStop script in the release 41 bundle. A DevOps engineer must make sure the drain step runs on every instance during Friday's deployment of release 41. Which solution will meet these requirements?

    Right. In the game that’s Renown for your SDLC Automation building.

    Not that one: C is right. In the game your SDLC Automation building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    A drain step that first appears in release 41 would therefore not run until the deployment after it, and nothing release 41 installs can be in place before ApplicationStop. Shipping a small revision ahead of Friday that carries only the new stop script makes that script the previous successful one, so Friday's release 41 deployment runs the drain before stopping the application.

    Written from docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html

  3. A company runs an application on 30 Amazon EC2 instances that operators set up by hand. Its files sit in /srv/app, and a process started from /srv/app runs on each instance. The company is moving releases to AWS CodeDeploy in-place deployments. The first revision holds newer versions of the files in /srv/app, and its deployment must replace them. On each instance, the running process must be stopped first, then the files replaced, then the process started again, with no manual work on the instances. Which combination of steps will meet these requirements? (Select TWO.)

    Right. In the game that’s Renown for your SDLC Automation building.

    Not that one: B,E is right. In the game your SDLC Automation building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    And ApplicationStop never runs the first time CodeDeploy deploys to an instance, because its script comes from a previous revision that does not exist yet, so the stop has to come from a hook in this revision that runs before Install, which is BeforeInstall.

    Written from docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting-deployments.html

Ask for something