Introducing Instance Refresh for EC2 Auto Scaling
This post is contributed to by: Ran Sheinberg – Principal EC2 Spot SA, and Isaac Vallhonrat – Sr. EC2 Spot Specialist SA
Today, we are launching Instance Refresh. This is a new feature in EC2 Auto Scaling that enables automatic deployments of instances in Auto Scaling Groups (ASGs), in order to release new application versions or make infrastructure updates.
Amazon EC2 Auto Scaling is used for a wide variety of workload types and applications. EC2 Auto Scaling helps you maintain application availability through a rich feature set. This feature set includes integration into Elastic Load Balancing, automatically replacing unhealthy instances, balancing instances across Availability Zones, provisioning instances across multiple pricing options and instance types, dynamically adding and removing instances, and more.
Many customers use an immutable infrastructure approach. This approach encourages replacing EC2 instances to update the application or configuration, instead of deploying into EC2 instances that are already running. This can be done by baking code and software in golden Amazon Machine Images (AMIs), and rolling out new EC2 Instances that use the new AMI version. Another common pattern for rolling out application updates is changing the package version that the instance pulls when it boots (via updates to instance user data). Or, keeping that pointer static, and pushing a new version to the code repository or another type of artifact (container, package on Amazon S3) to be fetched by an instance when it boots and gets provisioned.
Until today, EC2 Auto Scaling customers used different methods for replacing EC2 instances inside EC2 Auto Scaling groups when a deployment or operating system update was needed. For example,
UpdatePolicy within AWS CloudFormation,
create_before_destroy lifecycle in Hashicorp Terraform, using AWS CodeDeploy, or even custom scripts that call the EC2 Auto Scaling API.
Customers told us that they want native deployment functionality built into EC2 Auto Scaling to take away the heavy lifting of custom solutions, or deployments that are initiated from outside of Auto Scaling groups.
Introducing Instance Refresh in EC2 Auto Scaling
You can trigger an Instance Refresh using the EC2 Auto Scaling groups Management Console, or use the new
StartInstanceRefresh API in AWS CLI or any AWS SDK. All you need to do is specify the percentage of healthy instances to keep in the group while ASG terminates and launches instances. Also specify the warm-up time, which is the time period that ASG waits between instances that get refreshed via Instance Refresh. If your ASG is using Health Checks, the ASG waits for the newly launched instances to be healthy before it continues to the next instances.
Instance Refresh in action
To get started with Instance Refresh in the AWS Management Console, click on an existing ASG in the EC2 Auto Scaling Management Console. Then click the Instance refresh tab.
When clicking the Start instance refresh button, I am presented with the following options:
With the default configuration, ASG works to keep 90% of the instances running and does not proceed to refresh more instances if that percentage is not kept. ASG waits for the newly launched instances to transition into the healthy state, in addition to the configured warm-up time (300 seconds in this example) to pass before proceeding to the next instances.
I can also initiate the same action from the AWS CLI by using the following code:
After initializing the instance refresh process, I can see ongoing instance refreshes in the console:
The following image demonstrates how an active Instance refresh looks in the EC2 Instances console. Moreover, ASG strives to keep the capacity balanced between Availability Zones by terminating and launching instances in different Availability Zones at the same time.
Automate your workflow with Instance Refresh
You can now use this new functionality to create automations that work for your use-case.
To get started quickly, we created an example solution based on AWS Lambda. Visit the solution page on Github and see the deployment instructions.
Here’s an overview of what the solution contains and how it works:
- An EC2 Auto Scaling group with two instances running
- An EC2 Image Builder pipeline, set up to build and test an AMI
- An SNS topic that would get notified when the image build completes
- A Lambda function that is subscribed to the SNS topic, which gets triggered when the image build completes
- The Lambda function gets the new AMI ID from the SNS notification, creates a new Launch Template version, and then triggers an Instance Refresh in the ASG, which starts the rolling update of instances.
- Because you can configure the ASG with
LaunchTemplateVersion = $Latest, every new instance that is launched by the Instance Refresh process uses the new AMI from the latest version of the Launch Template.
See the automation flow in the following diagram.
We hope that the new Instance Refresh functionality in your ASGs allow for a more streamlined approach to launching and updating your application deployments running on EC2. You can now create automations that fit your use case. This allows you to more easily refresh the EC2 instances running in your Auto Scaling groups, when deploying a new version of your application or when you must replace the AMI being used. Visit the user-guide to learn more and get started.