Terraform for Auto Scaling EC2 Instances

Isuru SIriwardana
3 min readOct 20, 2021

Provisioning an EC2 instance may not be enough to handle dynamic performance needs. It is possible to increase the instance as required, however ideally this could have handled automatically. AWS EC2 Auto-scaling fulfills this requirement. AWS Auto-scaling allows us to automatically add or remove instances when certain thresholds are reached. There are two components that needs to be created in order to setup an auto-scaling group in AWS,

  • An AWS launch configuration: This is the component that defines AMIs and regions like configurations.
  • An auto-scaling group: This specifies the properties used to scale resources. For example the CPU threshold to be considered.

This solution architecture includes following components in order to provision EC2 instance with AWS auto scaling, and the complete code for the content covered in this solution architecture is available in this GitHub repository location>>

  • Three public and private subnets in three availability zones.
  • Expose the EC2 instances to the internet via an Internet Gateway.
  • Required routing associations in route tables.
  • AWS launch configuration to provision EC2 instances.
  • An auto scaling group to manage EC2 instances via the AWS launch configuration.

Code Organization

Code associated with this solution in the repository is organized as below,

|-- autoscaling.tf
|-- autoscalingpolicy.tf
|-- backend.tf
|-- internetgateway.tf
|-- key.tf
|-- provider.tf
|-- securitygroup.tf
|-- subnets.tf
|-- vars.tf
|-- versions.tf
|-- vpc.tf
  • autoscaling.tf: This defines two main components; aws launch configuration and aws autoscaling group. AWS launch configuration specifies the basic configuration of the EC2 instance, such as AMI ID, a name prefix and instance type. Then the auto-scaling group defines the how the auto scaling should be handled, for example required number of instances. Also it defines in which VPC these EC2 instances should be launched.
  • autoscalingpolicy.tf: This defines the metrics to be considered with auto-scaling. It defines two auto-scaling policies. One for scaling up and another for scaling down. In addition to that, it consist two Cloud Watch metric alarms that would alert when auto-scaling policies are in action.
  • backend.tf: This provides the configurations to keep the state remotely.
  • internetgateway.tf: Declares the internet gateway, the route table for public access, and routing rules for the public subnet.
  • key.tf: Declares the public key required to place in EC2 instance.
  • provider.tf: Declares the AWS terrform provider.
  • securitygroup.tf: Declares the security group associated with the VPC to define ingress and egress.
  • subnets.tf: Declares the public and private subnets of the VPC.
  • vars.tf: Declares all the variables to be used by the infrastructure code.
  • vpc.tf: Declares the virtual private cloud.

Solution Overview

This section briefly explains each component of the Terraform code placed in the solution that is necessary to have more understanding.

Auto scaling group

Auto scaling groups allows to logically group EC2 instances and handle scaling automatically. Automatic scaling can be done with a combination of periodic health checks and auto scaling policies.

Size of the auto scaling groups are determined by the desired capacity. Once the auto scaling group is launched it maintains the desired instance count. If one instance becomes unhealthy it will terminate the instance and spin up a new instance to meet the desired count.

Auto scaling policy

Auto scaling policy allows to define conditions based on various metrics to dynamically increase or decrease the instance count.

VPC

VPC simply stands for virtual private network. A VPC provides network level isolations to resources launched within it, which means all our resources will be located in our own network.

By deafult AWS will provide a default VPC for our use, but for small and medium use cases we can create our own VPC per region. Resources located in two VPCs can’t communicate with each other using their private IP addresses. But it is possible to connect two VPCs and make that happen, which is referred as VPC peering.

Internet Gateway

Internet gateway is the component in VPC that provides communication between the resources in VPC and the public internet. A typical internet gateway serves two purposes, it provides a target in VPC route tables for internet routable traffic, and provides network address translation for instances that have a public IP address assigned.

Deployment

Prerequisites

First step of the deployment is to generate a ssh key pair,

$ $ ssh-keygen -t rsa

Verify and change the variables defined in the vars.tf or override them using a variable file with the name terraform.tfvars. Pay attention to the AWS region and the AMIs.

Initialize the providers,

$ terraform init

Verify the changes,

$ terraform plan

Provision the resources,

$ terraform apply

Once everything is done, don’t forget to deprovision the resources,

$ terraform destroy

Source

Associated code is available in this repository path >>

--

--