Day 68 - Scaling with Terraform

Day 68 - Scaling with Terraform

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.

Prerequisites

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

📁 terraform/terraform-aws 
    --📄 terraform.tf 
    --📄 provider.tf 
    --📄 vpc.tf
    --📄 subnet.tf 
    --📄 internetgateway.tf 
    --📄 routetable.tf 
    --📄 securitygroup.tf
  1. Create a terraform.tf file to declare AWS providers required for the system.

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

  2. Create a provider.tf file to specify the AWS region required for instance.

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

  3. Create a vpc.tf file to set up VPC for the ec2 instance.

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

  4. Inside subnet.tf file create 2 public subnets for auto-scaling group.

     resource "aws_subnet" "public_subnet_1a" {
       vpc_id = aws_vpc.main.id
       cidr_block = "10.0.16.0/20"
       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.32.0/20"
       availability_zone = "us-east-1b"
       tags = {
         Name = "public-subnet-2"
       }
     }
    

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

     resource "aws_internet_gateway" "gw" {
       vpc_id = aws_vpc.main.id
       tags = {
         Name = "internet-getway"
       }
     }
    

  6. Create a routetable.tf file for routing public subnet to VPC.

     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
     }
    

  7. Create a securitygroup.tf file which will create a security group that allows SSH, HTTP, HTTPS, and egress traffic to the instances.

     resource "aws_security_group" "web_server" { 
       name_prefix = "web-server-sg" 
       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"] 
        } 
     }
    

TASKS

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:

Step 1: Now create a main.tf file. In your main.tf file, add the following code to create an Auto Scaling Group:

resource "aws_launch_configuration" "web_server_as" {
  image_id           = "ami-053b0d53c279acc90"
  instance_type = "t2.micro"
  security_groups = [aws_security_group.web_server.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.web_server.id]
   subnets = [aws_subnet.public_subnet_1a.id,aws_subnet.public_subnet_1b.id]
   listener {
    instance_port = 8000
    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_1a.id, aws_subnet.public_subnet_1b.id]
}

Step 2: Run terraform init command which will initialize and provide all prerequisite plugins.

Step 3: Now execute terraform plan command.

Step 4: Run terraform apply to create the Auto Scaling Group.

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

Step 6: You can verify Auto scaling group is created named as “web-server-asg” in the Auto Scaling Group dashboard.

Step 7: “web-server-asg” auto-scaling group details.

TASK 2: Test Scaling

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

Step 2: Select the Auto Scaling Group you just created and click on the “Edit” button.

Step 3: Increase the “Desired Capacity” to 3 and click on the “Update” button.

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

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

Step 6: Decrease the “Desired Capacity” to 1 and wait a few minutes for the extra instances to be terminated.

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

Step 8: Once you are done with the newly created instance we can use terraform destroy command which will delete the complete infrastructure.

Conclusion

In Conclusion, Scaling with Terraform provides a robust and efficient solution for managing infrastructure growth.

By leveraging Terraform's declarative configuration language, organizations can easily define, provision, and scale resources across various cloud providers and environments. This approach streamlines the process of adding or removing resources, ensuring consistency and reproducibility while minimizing manual intervention.

Overall, Terraform simplifies the complexities of scaling infrastructure, enabling teams to adapt quickly to changing demands and optimize resource utilization effectively.

Hope you find it helpful🤞 So I encourage you to try this on your own and let me know in the comment section👇 about your learning experience.✨

*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊

~Smriti Sharma✌