Deploy latest code on auto-scale new server start


So at the basic level, I have a launch template, autoscale group and the code in code-commit. I have a deployment group based on tag (this app doesn't require a target group), and when a developer commits that update, I can see the pipeline execute, one at a time updates the server, runs the after install script and were good.

So I am trying to understand if I change the autoscale group for 5 -> 6 servers how to tell that 6th server to get those updates before going into production. I am looking here -, and notice this line "You can also coordinate deployments in CodeDeploy with Amazon EC2 Auto Scaling instances registered with Elastic Load Balancing load balancers". This application doens't use any public facing ports so no need for an ELB/TargetGroup, so is that the only way to accomplish this or is there some on boot shell script that can run to automate this? I can build that in but seems a lot since the deploy already works perfect with the tag|resource, but want to try to keep this simple.

asked a year ago900 views
3 Answers

Thank you for that reply, but didn't answer the OP. In a simplified way to ask it;

If I have an autoscale group, I increase that count, what the best way for that instance to start, AND pull down the latest code from code-deploy?

The basic issue is I have 5 servers running version 1.0 from the launch template. I update code commit to version 1.1, it looks and says oh, here are 5 servers in that deployment group, it updates, all those are now version 1.1

Something triggers and the group says we need another server, it starts, using the launch template (which was on version 1.0). Before it goes into production, is that a way at the autoscale group to say check code-deploy and pull down the latest version.

answered a year ago
  • Added a new answer with the text from the documentation which is the exact process that occurs when the ASG scales out an achieves what you are looking to do. This is not tied to the launch template. Its part of the codedeploy process. The launch template has the EC2 configuration etc and not the codedeployed versions


If you read the documentation the ALB is optional.

You can also coordinate deployments in CodeDeploy with Amazon EC2 Auto Scaling instances registered with Elastic Load Balancing load balancers.

The link provided is correct and doesn’t require an ALB

You can follow the tutorial

profile picture
answered a year ago

As per documentation you have linked in the question, it uses a lifecycle hook to deploy the revisions of your code automaticlly to the new EC2's that launch. Its all part of the ASG and CodeDeploy lifecycle event.

How Amazon EC2 Auto Scaling works with CodeDeploy In order for CodeDeploy to deploy your application revision to new EC2 instances during an Auto Scaling scale-out event, CodeDeploy uses an Auto Scaling lifecycle hook. The lifecycle hook notifies CodeDeploy that an Auto Scaling scale-out event is in progress and that CodeDeploy must deploy a revision to the scaled-out instances.

After the lifecycle hook is installed, it is used during scale-out events. A scale-out event unfolds as follows:

  1. The Auto Scaling service (or simply, Auto Scaling) determines that a scale-out event needs to occur, and contacts the EC2 service to launch a new EC2 instance.
  2. The EC2 service launches a new EC2 instance. The instance moves into the Pending state, and then into the Pending:Wait state.
  3. During Pending:Wait, Auto Scaling runs all the lifecycle hooks attached to the Auto Scaling group, including the lifecycle hook created by CodeDeploy.
  4. The lifecycle hook sends a notification to the Amazon SQS queue that is polled by CodeDeploy.
  5. Upon receiving the notification, CodeDeploy parses the message, performs some validation, and starts to deploy your application to the new EC2 instance using the last successful revision.
  6. While the deployment is running, CodeDeploy sends heartbeats every five minutes to Auto Scaling to let it know that the instance is still being worked on.
  7. So far, the EC2 instance is still in the Pending:Wait state.
  8. When the deployment completes, CodeDeploy indicates to Auto Scaling to either CONTINUE or ABANDON the EC2 launch process, depending on whether the deployment succeeded or failed.
  • If CodeDeploy indicates CONTINUE, Auto Scaling continues the launch process, either waiting for other hooks to complete, or putting the instance into the Pending:Proceed and then the InService state.

  • If CodeDeploy indicates ABANDON, Auto Scaling terminates the EC2 instance, and restarts the launch procedure if needed to meet the desired number of instances, as defined in the Auto Scaling Desired Capacity setting.

profile picture
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions