This note continues my discussion on the Amazon EC2 Auto Scaling group that I started in my previous post, so please read that before this one. In that post, I explain the fundamentals of creating an Amazon EC2 Auto Scaling Group using Terraform. An Amazon Auto Scaling group consists of Amazon EC2 instances with specific properties like the instance type, the AMI ID, the subnet where the instances are placed, the user data script to run, etc. All of that information is stored in a launch template. An Auto Scaling group creates EC2 instances based on the specifications in the launch template.
Instance refresh is a functionality that enables auto-provisioning (creation) of new EC2 instances and de-provisioning (termination) of the older EC2 instances in an Amazon EC2 Auto Scaling group. Since EC2 instances in an Amazon Auto Scaling Group are created based on the specifications in a launch template, a change in the launch template can trigger an instance refresh event.
If you are new to this concept, you might have a question -Why should I care about an instance refresh? And here is my answer. A project team uses an Amazon EC2 Auto Scaling group to host applications, services, etc. An application or service is a continuously changing/evolving product based on customer feedback. If a project team hosts their applications/services on an Amazon EC2 Auto Scaling group, they specify the details in the launch template. The change could be, for example, in the form of a new AMI ID, a different instance type, or an updated user data script. A launch template consists of all that information. Hence, a project team would update the launch template as part of their application or service release. However, updating a launch template does not automatically deploy new Amazon EC2 instances. Yes, newly created instances in the Auto Scaling group would follow the new launch template due to a scale-out event or if existing instances got terminated. However, in the absence of that, the existing Amazon EC2 instances won’t be replaced unless that is explicitly called out in the Amazon EC2 Auto Scaling group properties. And that is what I demonstrate in this note. Here is a link to my GitHub repository for your reference.
Please note that an instance refresh can also be triggered manually, which adds the extra step of someone having to trigger that.
The properties to enable an Amazon Auto Scaling group instance refresh event with a launch template update using Terraform are specified in the aws_autoscaling_group
resource definition. Here is a link to the official Terraform registry for that. Two property blocks trigger the instance refresh. These are launch_template
and instance_refresh
.
Inside the
launch_template
property block, two values are specified: id and version. A direct dependency is created by specifying these values to a specific aws_launch_template
resource.
Then, inside the instance_refresh
property, the triggers
value is set to launch_template
, ensuring that the Amazon EC2 Auto Scaling group would trigger that as soon as the launch template is updated.
To test this theory, I updated the instance_type
from a t3.medium
to a t2.medium
and deployed the changes using the command terraform apply
. Then, on the AWS Console, I navigated to EC2 →Auto Scaling Groups → select the Auto Scaling group → Instance Refresh → Active instance refresh and saw that the process had started. Below is a screenshot of the same activity.
Then, under Instance refresh history, I saw the status of the activity.
And under the Activity Tab → Activity History, I saw the below messages as instances were updated:
Since I had the
min_healthy_percentage
set to 50% for the instance_refresh
preference property, the Auto Scaling Group ensured that at any point in time during the instance refresh, that many instances remained available.
Finally, under the Instance management tab, I saw that one of the Amazon EC2 instances ran with a t2.medium
instance type.
After a few minutes, all the Amazon EC2 instances were on the
t2.medium
instance type, and the instance refresh activity was complete. And that brings us to the end of this note. I also have a separate note on creating an Amazon EC2 Auto Scaling group with metric scaling policies using Terraform, which you might find helpful.
I hope this note was helpful, and please do not hesitate to comment if you have any questions or suggestions.