Skip to main content

Command Palette

Search for a command to run...

Day 68: Scaling with Terraform

Published
4 min read
Day 68: Scaling with Terraform
A

Hello, I am Akshay Phadke I have 10 Yrs of Experience in various Technologies. Currently, I am learning a few DevOps tools. I am working on Microsoft Technologies and AWS

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.

Create a provider file for AWS.

  • Create a file named provider.tf and add the following code to it:
provider "aws" {
  access_key = ""
  secret_key = ""
  region     = "us-east-1"
}

Create a Terraform version file.

  • Create a file named terraform.tf and add the following code to it:

  •   terraform {
        required_providers {
          aws = {
            source  = "hashicorp/aws"
            version = "~> 4.16"
          }
        }
        required_version = ">=1.2.0"
      }
    

    Create a VPC (Virtual Private Cloud) with CIDR block 10.0.0.0/16.

    • Create a file named vpc.tf and add the following code to it:
resource "aws_vpc" "main_example" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "main_example"
  }
}

Create a public subnet with CIDR block 10.0.1.0/24 and 10.0.2.0/24 in the above VPC.

  • Create a file named public_subnet.tf and add the following code to it:

      resource "aws_subnet" "public_subnet_1" {
        vpc_id     = aws_vpc.main_example.id
        cidr_block = "10.0.1.0/24"
    
        tags = {
          Name = "public subnet 1"
        }
      }
    
      resource "aws_subnet" "pubic_subnet_2" {
        vpc_id     = aws_vpc.main_example.id
        cidr_block = "10.0.2.0/24"
    
        tags = {
          Name = "public subnet 2"
        }
      }
    

    Create an Internet Gateway (IGW) and attach it to the VPC.

    • Create a file named igw.tf and add the following code to it:
    resource "aws_internet_gateway" "internet_gateway" {
      vpc_id = aws_vpc.main_example.id

      tags = {
        Name = "internet_gateway_devops"
      }
    }

Create a route table for the public subnet and associate it with the public subnet. This route table should have a route to the Internet Gateway.

  • Create a file named route_table.tf and add the following code to it:
    resource "aws_route_table" "route_table" {
      vpc_id = aws_vpc.main_example.id

      route {
        cidr_block = "0.0.0.0/0"
        gateway_id = aws_internet_gateway.internet_gateway.id
      }
      tags = {
        Name = "route_table_devops"
      }
    }

    resource "aws_route_table_association" "public_subnet_association_1a" {
      subnet_id      = aws_subnet.public_subnet_1.id
      route_table_id = aws_route_table.route_table.id
    }

    resource "aws_route_table_association" "public_subnet_association_1b" {

      subnet_id      = aws_subnet.pubic_subnet_2.id
      route_table_id = aws_route_table.route_table.id
    }

Let's create a Security Group for our EC2 instance.

  • Create a file named security_group.tf and add the following code to it:
    resource "aws_security_group" "web_server_grp" {
      name_prefix = "web-server-sg"
      vpc_id      = aws_vpc.main_example.id

      ingress {
        from_port   = 80
        to_port     = 80
        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"]
      }
    }

Task 1: Create an Auto Scaling Group.

  • Create a file named autoscaling.tf and add the following code to it:
    resource "aws_launch_configuration" "web_server_launch1" {
      name                        = "web_server_launch1"
      image_id                    = "ami-067d1e60475437da2"
      instance_type               = "t2.micro"
      security_groups             = [aws_security_group.web_server_grp.id]
      associate_public_ip_address = true

      user_data = <<-EOF
                    #!/bin/bash
                    echo "<html><body><h1>Welcome to Terraform</h1></body></html>" > index.html
                    nohup python -m SimpleHTTPServer 80 &
                    EOF
    }

    resource "aws_autoscaling_group" "web_server_asg1" {
      name                 = "web_server-asg"
      launch_configuration = aws_launch_configuration.web_server_launch1.name
      min_size             = 1
      max_size             = 3
      desired_capacity     = 2
      health_check_type    = "EC2"
      vpc_zone_identifier = [
      aws_subnet.public_subnet_1.id, aws_subnet.pubic_subnet_2.id]
    }

Now let's initialize the terraform.

terraform init
  • Now let's apply the terraform.

C

terraform apply

We can check the applied changes in the terminal.

  • Verify the changes in AWS Management Console by checking Auto Scaling Group.

  • Check the desired capacity of Auto Scaling Group.

Task 2: Test Scaling.

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

  • Step-01: Select the Auto Scaling Group you just created and click on the "Edit" button and increase the "Desired Capacity" to 3 and click on the "Save" button.

image

  • Step-02: Wait a few minutes for the new instances to be launched and verify that the new instances have been launched.
  • Step-03: Decrease the "Desired Capacity" to 1 and wait a few minutes for the extra instances to be terminated.

image

  • Step-04: Go to the EC2 Instances service and verify that the extra instances have been terminated.

More from this blog

Untitled Publication

80 posts