Skip to main content

Command Palette

Search for a command to run...

Day 68 - Scaling with Terraform

Updated
3 min read

Before proceeding, we have to set up AWS Provider, Region, VPC, InternetGateway, Security Group, and RouteTable.

Prerequisite : If you have not setup terraform please follow below blog:

  1. create terraform.tf
terraform {
    required_providers {
      aws = {
        source  = "hashicorp/aws"
        version = "~> 4.0"
      }
    }
  }
  1. create vpc.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_vpc" "main" {
    cidr_block       = "10.0.0.0/16"
    tags = {
      Name = "ajay-vpc"
    }
}
  1. create public_subnet.tf
resource "aws_subnet" "public_subnet" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
  availability_zone = "us-east-1a"


  tags = {
    Name = "Public Subnet"
  }
}

resource "aws_subnet" "public_subnet_1b" {
   vpc_id     = aws_vpc.main.id
   cidr_block = "10.0.3.0/24"
   availability_zone = "us-east-1b"
   tags = {
     Name = "public-subnet-2"
   }
 }
  1. create internet_gateway.tf
resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.main.id

  tags = {
    Name = "My IGW"
  }
}
  1. create public_route_table.tf
resource "aws_route_table" "public_route_table" {
  vpc_id = aws_vpc.main.id

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

  tags = {
    Name = "Public Route Table"
  }
}

resource "aws_route_table_association" "public_subnet_association" {
  subnet_id      = aws_subnet.public_subnet.id
  route_table_id = aws_route_table.public_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.public_route_table.id
}
  1. create ec2_security_group.tf
resource "aws_security_group" "ec2_security_group" {
  name        = "EC2 Security Group"
  description = "Allow SSH access"
  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"]
  }

  ingress {
     from_port   = 443
     to_port     = 443
     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"]
   }
}
  1. create as_group.tf
resource "aws_launch_configuration" "web_server_as" {
    image_id           = "ami-053b0d53c279acc90"
    instance_type = "t2.micro"
    security_groups = [aws_security_group.ec2_security_group.id]

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

  resource "aws_elb" "web_server_lb"{
     name = "web-server-lb"
     security_groups = [aws_security_group.ec2_security_group.id]
     subnets = [aws_subnet.public_subnet.id,aws_subnet.public_subnet_1b.id]
     listener {
      instance_port     = 80
      instance_protocol = "http"
      lb_port           = 80
      lb_protocol       = "http"
    }
    tags = {
      Name = "terraform-elb"
    }
  }

  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"
    load_balancers       = [aws_elb.web_server_lb.name]
    vpc_zone_identifier  = [aws_subnet.public_subnet.id, aws_subnet.public_subnet_1b.id]
  }

Now run terraform init then terraform plan

then terraform apply

Once Terraform is done with execution two new EC2 instances are created as desired capacity is set to 2.

You will see 1 auto scaling group named web-server-asg and 1 load balancer named web-server-lb

Now if you check your load balancer DNS URL:

That was all for today. If you have any queries/suggestions please write in comment. See you another day with another challenge.

More from this blog

Ajay Patel

116 posts