2

Just trying to get a bit of info on aws asg.

Here is my scenario;

  • launch an asg from a launch config using a default ubuntu ami
  • provision (install all required packages and config) the instances in the asg using ansible
  • deploy code to the instances using python

Happy days, every thing is setup.

The problem is if I should change the metrics of my asg so that all instances are terminated and then change it back to the original metrics, the new instances come up blank! No packages, No code!

1 - Is this expected behaviour?

2 - Also if the asg has 2 instances and scales up to 5 will it add 3 blank instances or take a copy of 1 of the running instances with code and packages and spin up the 3 new ones?

If 1 is Yes, how do I go around this? Do I need to use a pre-baked image? But then even that won't have the latest code.

Basically at off peak hours I want to be able to 'zero' out my asg so no instances are running and then bring then back up again during peak hours. It doesn't make sense to have to provision and deploy code again everyday.

Any help/advice appreciated.

moh_abk
  • 2,064
  • 7
  • 36
  • 65

2 Answers2

2

The problem happening with your approach is you are deploying the code to the launch instance. So when you change the ASG metrics instance close and come up again they are launched from the AMI so they miss the code and configuration. Always remember in auto scaling the newly launched instances are launched using the AMI the ASG is using and the launched instance.

To avoid this you can use user data that will run the configuration script and pull the data from your repo to the instance on each of the instance when the instance is getting launched from the AMI so that blank instance don't get launched.

Read this developer guide for User Data

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html

Piyush Patil
  • 14,512
  • 6
  • 35
  • 54
2
  1. Yes, this is expected behaviour
  2. It will add 3 blank instances (if by blank you mean from my base image)

Usual options are:

  1. Bake new image every time there is a new code version (it can be easily automated as a step in build/deploy process), but his probably makes sense only if that is not too often.

  2. Configure your instance to pull latest software version on launch. There are several options which all involve user-data scripts. You can pull the latest version directly from SCM, or for example place the latest package to a S3 bucket as part of the build process, and configure the instance on launch to pull it from S3 and self deploy, or whatever you find suitable...

Dusan Bajic
  • 10,249
  • 3
  • 33
  • 43
  • So If I pre-bake an ami, then use that in my asg with launch config using that ami, I have fixed my first issue which is packages and config. Now if I deploy code to the new instances let's say 3 and my service is up and running.. when asg scales to 5, will the 2 new instances have the code the other 3 have? – moh_abk Aug 21 '16 at 19:53
  • No. When you deploy code to active instances, at the same time bake the new ami. – Dusan Bajic Aug 21 '16 at 20:01
  • If you feel that it is too much overhead to build new AMI for each software version, you can go with option 2. – Dusan Bajic Aug 21 '16 at 20:53