Certopolis

AWS Certified DevOps Engineer – Professional

CodeDeploy with Auto Scaling groups: scaling during a deployment

When a deployment group includes an Auto Scaling group, CodeDeploy installs a launch lifecycle hook so that every new instance gets the current revision. The CodeDeploy User Guide adds an optional termination hook for shutdown scripts, and lets CodeDeploy suspend Terminate, AZRebalance, InstanceRefresh and ScheduledActions during an in-place deployment.

What do the launch and termination hooks do?

The launch hook tells CodeDeploy that a scale-out is in progress. CodeDeploy then runs a launch deployment that installs the revision on the new instance. The launch hook is always on and cannot be turned off.

The termination hook is optional. You turn it on with the 'Add a termination hook to Auto Scaling groups' option on the deployment group. On scale-in, the instance waits in Terminating:Wait while CodeDeploy runs a termination deployment. Nothing is deployed. CodeDeploy only generates lifecycle events, so a script on ApplicationStop can shut the application down cleanly.

While the termination deployment runs, CodeDeploy sends a heartbeat to Auto Scaling every five minutes. When it ends, CodeDeploy tells Auto Scaling to CONTINUE whether the scripts succeeded or failed, so a failed shutdown script does not block the termination.

What happens if the group scales during a deployment?

Instances launched while a deployment is underway get the previously deployed revision, not the new one. By default CodeDeploy then starts a follow-on deployment straight after the first to bring those outdated instances up to date. This default can be changed so that outdated instances are left on the older revision.

A scale-in during a deployment can terminate an instance before its lifecycle events finish. The deployment to that instance may fail, and the whole deployment fails if healthy instances drop below the minimum healthy hosts value. The termination hook does not prevent this race.

Which Auto Scaling processes can CodeDeploy suspend?

For an in-place deployment group that contains at least one Auto Scaling group, CodeDeploy can suspend Terminate, AZRebalance, InstanceRefresh and ScheduledActions, in any combination. It suspends them before deploying and resumes them when the deployment succeeds, fails or is stopped. Blue/green deployments are not supported.

Suspending Terminate prevents scale-in from removing instances that are being updated. Suspending ScheduledActions makes Auto Scaling skip scheduled actions that start during the deployment, and they are not run later. Scaling policies can still add instances, because CodeDeploy does not suspend Launch, HealthCheck or ReplaceUnhealthy.

The service role needs autoscaling:SuspendProcesses and autoscaling:ResumeProcesses. While Terminate is suspended, Auto Scaling does not replace unhealthy instances, and Spot interruptions still happen.

Where the exam catches people

  • Believing the termination hook stops scale-in during a deployment: it only runs shutdown scripts. To stop scale-in, have CodeDeploy suspend the Terminate process.
  • Believing CodeDeploy can suspend Launch to hold back scale-out: it cannot. Only Terminate, AZRebalance, InstanceRefresh and ScheduledActions can be suspended.
  • Believing a skipped scheduled action runs after the deployment: Auto Scaling skips it and does not run it later.
  • Believing an instance launched mid-deployment gets the new revision first: it gets the previous revision, and a follow-on deployment updates it.
  • Believing a failed termination deployment keeps the instance alive: CodeDeploy lets the termination proceed either way.

Written from these sources

Try 3 questions on this

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

  1. A company's pipeline releases an order service with AWS CodeDeploy in-place deployments to an Amazon EC2 Auto Scaling group, and a release takes about an hour. Two things add instances to the group: an action that raises the desired capacity to 30 at 07:30 every weekday, and a target tracking policy on CPU utilization. The operations team wants the 07:30 increase never to start while a release is in progress. Increases from the target tracking policy must keep working during releases. How should the DevOps engineer configure the deployment group to meet these requirements?

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

    Not that one: D 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

    With it chosen in the deployment group, CodeDeploy suspends it before the hour-long in-place deployment touches any instance and resumes it once the deployment succeeds, fails or is stopped. CodeDeploy cannot suspend Launch at all, so the target tracking policy can still add instances when CPU climbs in the middle of a release.

    Written from docs.aws.amazon.com/codedeploy/latest/userguide/integrations-aws-auto-scaling-suspend-processes.html

  2. A company releases an application with AWS CodeDeploy in-place deployments to an Amazon EC2 Auto Scaling group. Each instance buffers outgoing events on local disk. Whenever the group scales in, a shutdown script must flush that buffer to Amazon S3 before the instance is terminated, and a flush can take up to 20 minutes. If the script fails, the instance must still be terminated so that the scale-in completes without anyone stepping in. The script is already in every revision. What is the MOST operationally efficient way to meet these requirements?

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

    Not that one: D 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

    CodeDeploy sends Auto Scaling a heartbeat every five minutes while that deployment runs, so a 20-minute flush is not cut short, and when the deployment ends it tells Auto Scaling to continue the termination whether it succeeded or failed. The failure case therefore needs no extra machinery.

    Written from docs.aws.amazon.com/codedeploy/latest/userguide/integrations-aws-auto-scaling.html

  3. A company's pipeline releases an application with AWS CodeDeploy in-place deployments to an Amazon EC2 Auto Scaling group of 40 instances, and a release takes about 90 minutes. A target tracking policy scales the group out and in while releases run. Two problems need fixing. Scale-ins have terminated instances while CodeDeploy was still installing on them, and those releases failed. Today, instances that the policy launches during a release keep the old revision until an engineer deploys again; they must end up on the new revision without that step. 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: A,D 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

    Instances launched mid-release cannot be held back, since CodeDeploy does not suspend Launch, but that is not needed: they come up with the previous revision, and with the outdated instances strategy at its default of UPDATE CodeDeploy starts a follow-on deployment right after the release to bring them up to date.

    Written from docs.aws.amazon.com/codedeploy/latest/userguide/integrations-aws-auto-scaling.html

Ask for something