terraformaws-application-load-balancerlaunch-configurationaws-auto-scaling

How to ensure that the ASG ( Auto scaling Group) replaces existing instances with every change in the Launch configuration


The infrastructure is provisioned using terraform code. In our AWS environment, we have a new AMI created for every commit made to the repository. Now, if we want to have autoscaling configured for the web servers behind an ALB using this new AMI

Also, do you have any idea of how can we pro-grammatically (via terraform) get how many servers run at any point in time, in case of auto- scaling ?

Any help is highly appreciated here. Thanks!


Solution

  • For the most part this is pretty straightforward and there are already a dozen of implementations around the web.

    The tricky part is to express the 'create_before_destroy' field on the LC and the ASG. You schould also refer to the LC in your ASG resource. That way once your LC is changed you would trigger a workflow that creates a new ASG, that replaces your current one.

    Very Good Documented Example

    Also, do you have any idea of how can we pro-grammatically (via terraform) get how many servers run at any point in time, in case of auto- scaling ?

    This depends on the context. If you have a static number it's easy, you could define it in your module and stick with it. If it's about passing the previous ASG value the way would be again described in the guide above :) You need to write a custom external handler for how many in 'the moment' running instances you have around your target groups. There might be of course a new AWS REST API addition that gives you the chance to query all your Target Groups health check property and get their total sum ( not aware about it ). Then again, you might add some custom rules for scaling policies.

    External Handler

    Side note: in the example the deployment is happening with ELB.