How can I automatically cycle a new image in an AWS Auto Scaling Group?
I have a web application setup with a load balancer and auto scaling group to manage scaling. The source code is in a git repository so I don't have to update the images when the code changes, but occasionally the environment changes so we create a new image. Then that image needs to be cycled into the auto scaling group.
Is there a way to cycle the images automatically? Right now I schedule a scale up and scale down action which gets rid of the old instances.
Solution 1:
I'd like to suggest "AWS-HA-Release" to do this - the way AWS-HA-Release works:
- If the current autoscaling group and ELB report 5 instances that are healthy,
- AWS-HA-Release brings a new instance into production and waits for the ELB to identify it as healthy (bring the total number of healthy instances to 6)
- Removes an old instance (total down to 5)
- Brings another instance into service (total to 6)
- Removes an old instance (total down to 5)
- and so on until all instances have been replaced
In this case, you can ship new code or new AMI versions without downtime and have the benefit of entirely new instances. The AWS-HA-Release tool is available at https://github.com/colinbjohnson/aws-missing-tools.
Solution 2:
The easier way is to increase number of minimum instances in Auto-Scaling Group (ASG) to double of your current count, wait when all of them are started and then change that minimum number of instances down to what it was. ELB will kill older instances and will leave newer instances with code. To achieve that Termination policy should be set to 'OldestInstance' to work as intended. Default termination policy may have unwanted side effects.
You can look at AWS CLI parameters and examples here: http://docs.aws.amazon.com/cli/latest/reference/autoscaling/update-auto-scaling-group.html
Solution 3:
the way i manage this scenario is to use the UpdatePolicy feature of the AWS::AutoScaling::AutoScalingGroup object in cloud formation. when the cloud formation stack is updated it will manage the cycling of the instances.
some references. http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-group.html http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatepolicy.html
Solution 4:
Also have a look at the Netflix Asgard tool that is now open source. Not only can it set up Auto Scaling Groups, it can also perform rolling releases of new AMI images for a group of instances.