July 23, 2023

How to do rolling updates on EC2 Autoscaling Group in AWS

Rolling Updates, also known as Instance Refresh in AWS EC2 Auto Scaling groups, refers to updating the EC2 instances, that are part of the Autoscaling group, such as to update any of their configurations, such as user-data, updating the version of software installed on the instances, or to add something new to their user data

Time and again we run into requirements to update the instances, such that it avoids downtime as much as possible and be as hassle free as possible. Thus, performing a rolling Update helps us quite often

Please note that any method highlighted in this how-to should not be replicated as is in Production workloads

Thus, how about we go to perform a rolling update ? Use the steps below to get an idea of what is involved in doing so

Setup

For this tutorial, I am choosing a very basic set of infrastructure, which includes the following:

  • 1 VPC
  • 2 Subnets
  • 1 Security Group
  • 1 Application Load Balancer
  • 1 AutoScaling Group consisting of the following configuration:
    • Max Size: 5
    • Min Size: 2
    • DesiredCapacity: 2

You can see the whole infrastructure in the below diagram

For the purpose of this blog post, I am going to use a CloudFormation Template. Github Repo here

The CloudFormation Template is looks as below

https://gist.github.com/abdulrafaykhan/eba48932464827e8b406cac2f8114b6e

Now, for the sake of of this blog post, I will show you how we can update the user data manually and then update the instances one by one

Step 1: Update the User Data

If I go to the public domain name of the ALB, it currently shows the following:

As you can see, it shows this text. Now we will update the user data and eventually update the text

Thus, head over to the Launch Template and lets update the user data

Now once you go to the "Modify Template" you will see the following screen

Scroll down to see "Advanced details"

Expand and scroll down to see the user - data

Now we are going to change the text that is appearing in the index.html file

As you can see in the below snapshot, we have updated the user data as follows:

Thus, go ahead and select "Create Template Version"

Once the version has been successfully updated, you can view this in the launch template that there is a new version of the launch template

Step 2: Updating the Launch Template in Autoscaling Console

Now since we have updated the launch template, we need to update the AutoScaling group to use the new launch template version

Thus, head over to the AutoScaling Console and update the launch template

Scroll down to the Launch Template and update the Version from 1 to 2

Scroll down the press the Update to update the Autoscaling Group

As you can see, the launch template version has been updated to version 2

Step 3: Perform the Rolling Update

Now, we need to perform the rolling update on the instances

Thus, we need to first spin up new instances, which will be based on the new launch template and then de-commission the old one

Thus, go ahead in the Autoscaling config and update the desired from 2 to 4

Now, here, our maximum capacity goes up to 5 instances. In some cases, we also would need to update the maximum capacity to accomodate creating new instances

Thus, once done, press the Update button

Now we need to update the termination policy so that when we scale down to 2 instances, we terminated the oldest instances

Thus, scroll down in the Autoscaling console and edit the Advanced Configurations

Now on this screen you can configure multiple things such as scale-in protection, suspended processes and termination policies

Thus, update the termination policy to OldestInstance

Once done, press Update

Now, head over to the EC2 Instances console and see if the new instances are ready to process the workload

Now as you can see , there are 02 sets of instances on the basis of launch time. Also, the new instances seem healthy and ready to be rotated

Thus, head over to the Auto-Scaling console and scale it down to 02 instances

Once done, update the configuration

Now if you head over to Activity tab you can see the events

Since we are using a load-balancer, AWS knows that there might be some existing connections that could result in nasty crashes if it abruptly ended the connection

Thus, connection draining exists to help the load balancer to complete in-flight request made while keeping the existing connections open and preventing any new requests being sent to the instances that are de-registering or unhealthy (Jayendra's Cloud Blog)

Thus, by default, there is connection draining time frame of 5 mins. You can change this in the AutoScaling configs

Once the connection draining has finished, the old instance will be terminated and you can see this in the Activity tab for the Auto-scaling console

Now if you head over to the EC2 Console you can see the older instances have been terminated.

Also, if you navigate to the public DNS of the load balancer, you will see that the page has also update successfully

Thus, at this point, you have successfully performed the rolling update of the EC2 instances

Please ensure to delete the resources (EC2 instances, auto-scaling group, ALB, etc.) before moving away from the AWS Console to not get bankrupted by the AWS costs :)

Hope this blog post helps you get some understanding behind the process of rolling update, which enables you to update the configurations with the least amount of downtime :)

Happy AWSing !