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.
I'd like to suggest "AWS-HA-Release" to do this - the way AWS-HA-Release works:
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.
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
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
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.
As of 6/16/2020 EC2 AutoScaling supports Instance Refresh - a single API call to replace every instance in an ASG safely and autonomously.
To use it, update your ASG, setting a Launch Configuration or Launch Template containing your desired AMI, and then call StartInstanceRefresh.
https://aws.amazon.com/blogs/compute/introducing-instance-refresh-for-ec2-auto-scaling/
There's not actually a really good way of doing it to be honest. The best way I found to do it is to put a version in the ASG name. Any time I update the AMI I create a new ASG + Launch Config with the new version so it does not conflict with the other group. Then I terminate all the instances in the old group.
If a more fault tolerant deployment is required I recommend adding another step by involving the creation of a new loadbalancer as well. This allows you to have both ASG isolated from each other. It also allows you to have a "Staging" area to test your changes one last time before updating. Then when you're ready to switch you update the DNS records and terminate all the instances in the old group.
As I posted here (similar question, just with Terraform), it isn't built into ASG in any way, except if you use cloudformation. I struggled with it as well, so ended up writing a "roller" that watches multiple ASG, checks their state and updates. Always happy to take feedback. http://github.com/deitch/aws-asg-roller