Currently, we have a single production server running on an EC2 instance. Since we are getting a lot of traffic, we would like to use the Autoscaling feature, but we are kind of new to this.

Following is my understanding:
- We create a custom AMI from our existing production server
- Create a launch configuration from this AMI
- Create an AutoScaling group with this launch configuration
- Set conditions, if CPU usage is greater than 60%, add an instance, if less than 40% terminate 1 instance.
- Add a load balancer to this autoscaling group
- Now as far as I understood, to deploy new code to the auto scaling group, we have to create a new AMI with our updated application version and change the AMI used in the autoscaling group to this new one. Then terminate all the previous instances and create new ones from the updated AMI.

1. To create the updated AMI, do I have to keep our previous EC2 instance that is not part of the auto scaling group?
Deploy the new code there and create an AMI from that instance?
2. Or instead of creating new AMIs, we could just ssh into every server and pull new code from there? but the new instance that would spawn would be using the previous code.
3. Also, after adding the new AMI, how do we properly terminate all the previous instances?
4. Finally, How do we properly automate this?

  • Hi, if the answer below was useful please upvote and accept it. That’s the SF way of saying thanks :) – MLu Jan 31 at 19:50

Never rely on ssh'ing to the newly created instances - they can come and go at any time.

You can automate the instance setup with the Launch Configuration User Data where you can run any bash script you like. That script can install and configure your web server and pull any code. However if later on you update the code you'll have to SSH to every instance to deploy the new version. So that's not an optimal strategy.

Best to use a tool like AWS CodeDeploy than can do it all for you. With code deploy the process works like this:

  1. Create your AMI with a webserver preconfigured (or use the above mentioned UserData to configure it at launch). Note that at this point you only have the webserver, not the actual website code.

  2. Start CodeDeploy agent automatically at the end of UserData script.

  3. The agent will connect to CodeDeploy server and pull the latest website files (PHP, Java, ...).

  4. If you develop a new version of the website (e.g. a new Java WAR file) you'll instruct CodeDeploy to push it to all the currently active nodes.

This way the hosts that join the Auto Scaling Group automatically get the latest website code and you won't have to rebuild your AMIs all the time.

Hope that helps :)

| improve this answer | |
  • Oh nice, so this way we will only have to create a single AMI. Will try it out and will write an update. Thanks. :) – Pratish Shrestha Apr 4 '19 at 5:16
  • In my case, deploying a new code with all the deployment steps takes about 10 mins. So, we prefer to create / update AMI with already deployed code. Currently, we are doing this manually but would like to update the AMI through some script / automated way. – Satish Gadhave Jan 19 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.