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
Terminateprocess. - Believing CodeDeploy can suspend
Launchto hold back scale-out: it cannot. OnlyTerminate,AZRebalance,InstanceRefreshandScheduledActionscan 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.