Day 68 - Scaling with Terraform ๐Ÿš€

Day 68 - Scaling with Terraform ๐Ÿš€

ยท

4 min read

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:

  • In your main.tf file, add the following code to create an Auto Scaling Group:

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

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

resource "aws_vpc" "day68_scale_vpc" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "day68-scale_vpc"
  }
}

resource "aws_subnet" "day68_scale_public_subnet" {
  vpc_id     = aws_vpc.day68_scale_vpc.id
  cidr_block = "10.0.1.0/24"

  tags = {
    Name = "day68-scale_public_subnet"
  }
}

resource "aws_subnet" "day68_scale_private_subnet" {
  vpc_id     = aws_vpc.day68_scale_vpc.id
  cidr_block = "10.0.2.0/24"

  tags = {
    Name = "day68-scale_private_subnet"
  }
}

resource "aws_internet_gateway" "day68_scale_igw" {
  vpc_id = aws_vpc.day68_scale_vpc.id

  tags = {
    Name = "day68-scale_igw"
  }
}

resource "aws_route_table" "day68_scale_routetable" {
  vpc_id = aws_vpc.day68_scale_vpc.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.day68_scale_igw.id
  }

  tags = {
    Name = "day68-scale_routetable"
  }
}

resource "aws_route_table_association" "public_subnet_association" {
  subnet_id      = aws_subnet.day68_scale_public_subnet.id
  route_table_id = aws_route_table.day68_scale_routetable.id
}

resource "aws_key_pair" "day66_key" {
  key_name   = "day66-key"
  public_key = file("/home/ubuntu/.ssh/day66-key.pub")
}

resource "aws_security_group" "day68_scale_sg" {
  name_prefix = "day68-scale_sg"
  vpc_id      = aws_vpc.day68_scale_vpc.id

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

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

  ingress {
    from_port   = 22
    to_port     = 22
    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"]
    ipv6_cidr_blocks = ["::/0"]
  }
}

resource "aws_launch_configuration" "web_server_lc" {
  name_prefix          = "web-server-lc"
  image_id             = "ami-04b70fa74e45c3917"
  instance_type        = "t2.micro"
  key_name             = "day66-key" # You can use a previous key if you are doing it in the same instance, or if you are doing it in a new instance, you need to create a new key pair.
  security_groups      = [aws_security_group.day68_scale_sg.id]
  associate_public_ip_address = true

  user_data = <<-EOF
                #!/bin/bash
                sudo apt update
                sudo apt install -y apache2
                sudo systemctl start apache2
                sudo systemctl enable apache2
                echo "<html><body><h1>Welcome to my DevOpsParthu Community!</h1></body></html>" > /var/www/html/index.html
                sudo systemctl restart apache2
                EOF
}

resource "aws_autoscaling_group" "web_server_asg" {
  launch_configuration = aws_launch_configuration.web_server_lc.id
  min_size             = 1
  max_size             = 3
  desired_capacity     = 2
  health_check_type    = "EC2"
  vpc_zone_identifier  = [aws_subnet.day68_scale_public_subnet.id]
  force_delete         = true

  tag {
    key                 = "Name"
    value               = "web-server-asg"
    propagate_at_launch = true
  }
}
  • Run terraform init and terraform apply to create the autoscaling group using the Terraform configuration.

  • Verify the EC2 instance has been successfully created.

Task 2: Test Scaling

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

  • In the left side panel, scroll down until you see the autoscaling group. Click on it and then click on the autoscaling group you created.

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

  • Navigate to Group details and click on the Edit button.

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

  • Change the Desired capacity to 3.

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.

  • Navigate to the EC2 Instances service and verify that our 3 instances have been created.

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

  • Now modify the autoscaling group size to 1 desired capacity.

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

  • Navigate 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

Thanks For Reading! :)

-SriParthu๐Ÿ’๐Ÿ’ฅ

ย