Scaling with Terraform ๐Ÿš€

Scaling with Terraform ๐Ÿš€

Day 68 of #90DaysOfDevOps

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.

Before creating auto scalling group, there are a few requirements that have to be full filled. And those are following:

  • Create VPC

  • Create Public subnets under main vpc with multiple availability zones

  • Create internet gatewat under main vpc

  • Create route table for public subnet with internet gateway

  • Associate route table with public subnets

  • Create security group

# Variables
variable "awsRegion" {
  default = "us-east-1"
}

variable "ami" {
  default = "ami-0005e0cfe09cc9050" # Amazon Linux
}
variable "instanceType" {
  default = "t2.micro"
}

variable "keyName" {
  default = "my-linux-key"
}

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

# Create Public subnet under main vpc for us-east-1a
resource "aws_subnet" "public_subnet_1a" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.1.0/24"
  availability_zone = "${var.awsRegion}a"
  tags = {
    Name = "Public Subnet ${var.awsRegion}a"
  }
}

# Create Public subnet under main vpc for us-east-1b
resource "aws_subnet" "public_subnet_1b" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.2.0/24"
  availability_zone = "${var.awsRegion}b"
  tags = {
    Name = "Public Subnet ${var.awsRegion}b"
  }
}

# Create internet gatewat under main vpc
resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.main.id
  tags = {
    "Name" = "Internet GateWay"
  }
}

# Create route table for public subnet with internet gateway
resource "aws_route_table" "route_table_public" {
  vpc_id = aws_vpc.main.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw.id
  }
  tags = {
    Name = "Route Table"
  }
}

# associate route table with public subnets
resource "aws_route_table_association" "public-public_subnet_1a" {
  subnet_id      = aws_subnet.public_subnet_1a.id
  route_table_id = aws_route_table.route_table_public.id
}

resource "aws_route_table_association" "public-public_subnet_1b" {
  subnet_id      = aws_subnet.public_subnet_1b.id
  route_table_id = aws_route_table.route_table_public.id
}

# Create security group with inbound and outbound ports open under main VPC
resource "aws_security_group" "web_server" {
  vpc_id      = aws_vpc.main.id
  name_prefix = "HTTP-SSH-Access"
  # inbound port 80
  ingress {
    description = "HTTP"
    from_port   = 80
    to_port     = 80
    cidr_blocks = ["0.0.0.0/0"]
    protocol    = "tcp"
  }
  # inbound port 22
  ingress {
    description = "SSH"
    from_port   = 22
    to_port     = 22
    cidr_blocks = ["0.0.0.0/0"]
    protocol    = "tcp"
  }
  # outbound port all
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

In your terraform file, add the following code to create an Auto Scaling Group:

# A launch config with server installed
resource "aws_launch_configuration" "web_server_as" {
  image_id        = "ami-0c7217cdde317cfec"
  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
}

# Auto scalling group
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]
}

Next, run command terraform init and terraform apply

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 "Save" 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 2 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.


GitHub repository for code: https://github.com/neelsoni26/aws-auto-scalling-terraform

Thank you for reading.

ย