Certopolis

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 ValidateService exists for Lambda deployments: Lambda has only BeforeAllowTraffic and AfterAllowTraffic, and they run Lambda functions, not scripts on an agent.
  • Believing BeforeAllowTraffic is the first ECS hook that can test the new tasks: AfterAllowTestTraffic runs earlier, once the test listener serves the replacement task set.
  • Believing a fixed ApplicationStop script in the new revision runs on the next deployment: ApplicationStop uses 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 Succeeded status under OneAtATime means every instance was updated: a failure on the last instance alone still reports success.

Written from these sources

Try 3 questions on this

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

  1. A developer deploys an application to Amazon EC2 instances by using an AWS CodeDeploy in-place deployment. On each instance, after the application process has been restarted, a smoke-test script must confirm that the deployment succeeded before the deployment is marked complete. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Testing and Deployment building.

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

    Show the full explanation and doc quotes

    It runs after ApplicationStart, so the restarted application process is available for the smoke-test script to exercise before CodeDeploy marks the deployment complete on the instance.

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

  2. A company uses AWS CodeDeploy to shift production traffic to new AWS Lambda function versions. Before any production traffic reaches a newly deployed version, an integration test suite must run against it, and the deployment must roll back automatically if the tests fail. How should the developer configure the AppSpec file to meet these requirements?

    Right. In the game that’s Renown for your Testing and Deployment building.

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

    Show the full explanation and doc quotes

    A validation function on BeforeAllowTraffic runs the tests first, then calls PutLifecycleEventHookExecutionStatus with Succeeded or Failed; a Failed status (or one hour without any report) fails the lifecycle event, which stops the deployment and triggers rollback.

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

  3. A company deploys an Amazon ECS service by using AWS CodeDeploy blue/green deployments. The deployment group defines a test listener in addition to the production listener. A validation AWS Lambda function must run at the earliest lifecycle event at which the test listener is serving traffic to the replacement task set, and a test failure must trigger a rollback. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Testing and Deployment building.

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

    Show the full explanation and doc quotes

    AfterAllowTestTraffic is defined as running after the test listener serves traffic to the replacement task set — the earliest point at which validation traffic can reach the new tasks — and a Failed result reported there triggers a rollback before production traffic ever shifts.

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

Ask for something