Day 68 - Scaling with Terraform 🚀

Day 68 - Scaling with Terraform 🚀

Yesterday, we learned how to AWS S3 Bucket with Terraform. Today, we will see how to scale our infrastructure with Terraform.

Understanding Scaling

Scaling is the process of adding or removing resources to match the changing demands of your application. As your application grows, you will need to add more resources to handle the increased load. And as the load decreases, you can remove the extra resources to save costs.

Terraform makes it easy to scale your infrastructure by providing a declarative way to define your resources. You can define the number of resources you need and Terraform will automatically create or destroy the resources as needed.

Task 1: Create an Auto Scaling Group

Auto Scaling Groups are used to automatically add or remove EC2 instances based on the current demand. Follow these steps to create an Auto Scaling Group:

Before starting the Autoscaling group, we need to set up an EC2 Instance with VPC, subnet, Internet gateway, routing ,Security group and user data

Create a terraform.tf to add the providers

terraform {
   required_providers {
     aws = {
       source  = "hashicorp/aws"
       version = "~> 4.0"
     }
   }
 }

Create a provider.tf file to specify the region

provider "aws" {
   region = "us-east-1"
}

Create a vpc.tf file to set up the VPC for our instances

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "main"
  }
}

Create 2 public subnets with different zones in a subnet.tf file. We will later use these subnets for multi-subnet initialization in our auto-scaling group.

 resource "aws_subnet" "public_subnet_1a" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.0.0/24"
  availability_zone = "us-east-1a"
  tags = {
    Name = "public subnet 1"
  }
}

resource "aws_subnet" "public_subnet_1b" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.1.0/24"
   availability_zone = "us-east-1b"
  tags = {
    Name = "public subnet 2"
  }
}

Create an internetgateway.tf file to configure the internet gateway.

 resource "aws_internet_gateway" "gw" {
  vpc_id = aws_vpc.main.id

  tags = {
    Name = "internet-gateway"
  }
}

Set up routing configurations in a routetable.tf file.

 resource "aws_route_table" "route_table" {
  vpc_id = aws_vpc.main.id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.gw.id
  }
  tags = {
    Name = "route_table"
  }
}

resource "aws_route_table_association" "public_subnet_association_1a" {
  subnet_id      = aws_subnet.public_subnet_1a.id
  route_table_id = aws_route_table.route_table.id
}
resource "aws_route_table_association" "public_subnet_association_1b" {
  subnet_id      = aws_subnet.public_subnet_1b.id
  route_table_id = aws_route_table.route_table.id
}

Create a security group.tf file to allow SSH, HTTP, and egress traffic to the instances.

resource "aws_security_group" "web_server" {
  name        = "web-server-sg"
  description = "Allow SSH and HTTP access from anywhere"
  vpc_id = aws_vpc.main.id
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
  }

Create main.tf add the following code to create an Auto Scaling Group

resource "aws_launch_configuration" "web_server_as" {
  image_id        = "ami-005f9685cb30f234b"
  instance_type  = "t2.micro"
  security_groups = [aws_security_group.web_server.id]

  user_data = <<-EOF
              #!/bin/bash
              echo "<html><body><h1>You're doing really Great</h1></body></html>" > index.html
              nohup python -m SimpleHTTPServer 80 &
              EOF
}

resource "aws_autoscaling_group" "web_server_asg" {
  name                 = "web-server-asg"
  launch_configuration = aws_launch_configuration.web_server_as.name
  min_size             = 1
  max_size             = 3
  desired_capacity     = 2
  health_check_type    = "EC2"
  vpc_zone_identifier  = [aws_subnet.public_subnet_1a.id, aws_subnet.public_subnet_1b.id]
}

Run terraform apply to create the Auto Scaling Group.

This Terraform code creates two AWS resources: an Autoscaling group and a launch configuration.

The launch configuration specifies the image ID, instance type, security group, and user data to be used when launching new instances. The user data script installs a simple web server and serves a basic HTML page with the message "You're doing Great" when an instance is launched.

The autoscaling group defines the parameters for the group, including the launch configuration to use, minimum and maximum number of instances, and desired capacity.

aws_launch_configuration:- This resource creates a launch configuration for EC2 instances that we are going to deploy as part of our autoscaling group.

The following arguments are required:

  • image_id - The EC2 image ID to launch.

  • instance_type - The size of the instance to launch.

  • security_groups - A list of associated security group IDS.

aws_autoscaling_group:- Provides an Auto Scaling Group resource.

  • max_size - Maximum size of the Auto Scaling Group.

  • min_size - Minimum size of the Auto Scaling Group.

  • desired_capacity - Number of Amazon EC2 instances that should be running in the group.

Below, two new EC2 instances were created as desired capacity was set to 2.

A launch configuration is successfully created under Autoscaling

The auto-scaling group is created. And two instances are running under web-server-asg auto-scaling group.

Task 2: Test Scaling

Go to the AWS Management Console and select the Auto Scaling Groups service.

Select the Auto Scaling Group you just created and click on the "Edit" button.

Increase the "Desired Capacity" to 3 and click on the "Update" button.

Wait a few minutes for the new instances to be launched.

Go to the EC2 Instances service and verify that the new instances have been launched.

Decrease the "Desired Capacity" to 1 and wait a few minutes for the extra instances to be terminated.

Go to the EC2 Instances service and verify that the extra instances have been terminated.

Congratulations🎊🎉 You have successfully scaled your infrastructure with Terraform.

Happy Learning :)