Terraform: Autoscale with AWS ALB

Girish V P
ADIBI Technologies, Basavanagudi, Blore.
3 min readMar 7, 2024

These are not new concepts and I guess, no explanation required. Let us get into the matter. This setup is done for California AWS Region (us-west-1). This is a very basic setup to create autoscaling group with essential resources including Application Load Balancer.

Pre-requsite

1 - An AMI from which instance is launched can give rise web service
2 - A VPC with two public subnet (for launching ALB) and two private
subnets (Launching EC2 instances )
3 - Security group which has public access to port 80 (for ALB), and another
one for EC2 instance connectivity
4 - .pem key

Step 1: Create a terraform AWS Provider. create a provider.tf terraform code file like below

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}

# Configure the AWS Provider
provider "aws" {
region = "us-east-1"
access_key = "Your Access Key"
secret_key = "Your Secret Key"
}

Step 2: Create a Launch template which contains attributes for EC2 instances.

resource "aws_launch_template" "my-launch-template-1" {
name_prefix = "my-lt"
image_id = "ami-XXXXXXX"
instance_type = "t2.micro"
key_name = "Key_Name"
tag_specifications {
resource_type = "instance"
tags = {
Name = "AS-Instance"
}
}

placement {
availability_zone = "us-west-1a"
}
network_interfaces {
subnet_id = "subnet-XXXXXXXX" # Private subnet ID
security_groups = [ "sg-XXXXXXXX" ]
}

block_device_mappings {
device_name = "/dev/xvda"
ebs {
volume_size = 8
}
}
}

Step 3: Create ALB. Use public subnets here

resource "aws_lb" "my-alb-1" {
name = "my-alb"
internal = false
load_balancer_type = "application"
security_groups = [ "sg-YYYYYYYY" ]
subnets = ["subnet-YYYYYYY","subnet-ZZZZZZZZ"]
enable_deletion_protection = false
enable_cross_zone_load_balancing = true
}

Step 4: Create Target Group for the ALB

resource "aws_lb_target_group" "my-tg-1" {
name = "my-target-group"
port = 80
protocol = "HTTP"
target_type = "instance"
vpc_id = "vpc-XXXXXXXX"
health_check {
}
}

Step 5: Create a Listener and Link to the ALB. This will forward the requests to the Target Group in the code

  resource "aws_lb_listener" "my-alb-listener-1" {
load_balancer_arn = aws_lb.my-alb-1.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.my-tg-1.arn
}
}

Step5 : Create Auto Scaling Group. Use the Private Subnet here. Include ALB Target Group

resource "aws_autoscaling_group" "my-asg-1" {
name = "my-autoscaling-group"
desired_capacity = 1
max_size = 2
min_size = 1
health_check_type = "EC2"
termination_policies = ["OldestInstance"]
vpc_zone_identifier = ["subnet-XXXXXXX","subnet-BBBBBBBB"]
target_group_arns = [aws_lb_target_group.my-tg-1.arn]
launch_template {
id = aws_launch_template.my-launch-template-1.id
version = "$Latest"
}
}

Step 6: Execute terraform apply command to create AWS resources.

Step 7: Now access the load balancer fro the web browser. We should be able to your web page. For me it is like below

Conclusion:

Creating a Auto scaling group with Application Load balancer is an easy process with terraform.

Disclaimer: It is recommended to do a thorough test before applying in production environment.

--

--