Day 68: Scaling with Terraform

Day 68: Scaling with Terraform

Introduction

In the ever-evolving landscape of cloud computing, the ability to scale infrastructure dynamically is paramount. In this guide, we will explore the intricacies of scaling with Terraform, a powerful infrastructure-as-code (IaC) tool. Specifically, we'll dive into creating an Auto Scaling Group using Terraform and thoroughly test its scaling capabilities.

Task 1: Creating an Auto Scaling Group with Terraform

1.1 Understanding the Terraform File

Our journey begins with the Terraform file, the blueprint that defines our scalable infrastructure. Below is a snippet of the Terraform file (main.tf) highlighting key resources.

provider "aws" {
  region = "us-east-1" # Change this to your desired AWS region
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16" # Change this to your desired CIDR block
}

resource "aws_security_group" "web_server" {
  name        = "web-server-sg"
  description = "Security group for web server"

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

  vpc_id = aws_vpc.main.id
}

resource "aws_subnet" "public_subnet_1a" {
  availability_zone = "us-east-1a" # Change this to your desired AZ
  cidr_block        = "10.0.1.0/24" # Change this to your desired CIDR block
  vpc_id            = aws_vpc.main.id
}

resource "aws_subnet" "public_subnet_1b" {
  availability_zone = "us-east-1b" # Change this to your desired AZ
  cidr_block        = "10.0.2.0/24" # Change this to your desired CIDR block
  vpc_id            = aws_vpc.main.id
}

resource "aws_elb" "web_server_lb" {
  name               = "web-server-lb"
  availability_zones = ["us-east-1a", "us-east-1b"] # Change these to your desired AZs

  listener {
    instance_port     = 80
    instance_protocol = "http"
    lb_port           = 80
    lb_protocol       = "http"
  }
}

resource "aws_launch_template" "web_server_lt" {
  name = "web-server-lt"

  image_id      = "ami-005f9685cb30f234b"
  instance_type = "t2.micro"
  user_data     = base64encode(<<-EOF
              #!/bin/bash
              echo "<html><body><h1>You're doing really Great</h1></body></html>" > /var/www/html/index.html
              nohup python -m SimpleHTTPServer 80 &
            EOF
  )
}

resource "aws_autoscaling_group" "web_server_asg" {
  name                    = "web-server-asg"
  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]
  launch_template {
    id = aws_launch_template.web_server_lt.id
  }
}

1.2 Setting up the Environment

Before we dive into Terraform, ensure your AWS credentials are set up.

1.3 Launching the Auto Scaling Group

Execute the following commands to initialize and apply the Terraform configuration:

terraform init
terraform apply

Review the changes prompted by Terraform and confirm the creation of resources.

1.4 Verification

Visit the AWS Management Console to visually verify the creation of resources, including the Auto Scaling Group, Launch Configuration, and EC2 instances.

1.5 Testing Auto Scaling

Now, let's test the Auto Scaling capabilities. Modify the "Desired Capacity" in the AWS Management Console, and observe the dynamic creation and termination of instances in the EC2 Instances service.

Task 2: Testing Scaling

2.1 Navigating the AWS Console

Familiarize yourself with the AWS Management Console and locate the Auto Scaling Groups service.

2.2 Adjusting Auto Scaling Group Configuration

  • Select the Auto Scaling Group.

  • Click "Edit" and increase "Desired Capacity" to 3.

  • Save the changes.

2.3 Observing Instance Creation

Observe the launch of new instances in the EC2 Instances service as the "Desired Capacity" is increased.

2.4 Scaling Down

Decrease the "Desired Capacity" to 1 to initiate the graceful termination of surplus instances.

2.5 Confirming Instance Termination

Visit the EC2 Instances service to confirm the successful termination of excess instances.

Conclusion

This comprehensive guide has walked you through the essential steps of creating and testing an Auto Scaling Group with Terraform. From understanding the Terraform file to real-world testing, you now have a solid foundation for building scalable infrastructure. As you continue your Terraform journey, explore advanced features and best practices to elevate your infrastructure-as-code skills. Happy scaling!

Follow me on LinkedIn.

Checkout my GitHub profile.