Day 68 - Scaling with Terraform 🚀   (Feb 2, 2024)

Day 68 - Scaling with Terraform 🚀 (Feb 2, 2024)

🙏 Introduction:

In this blog, we will learn how to scale our infrastructure with Terraform.

🔶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. Follow these steps to create an Auto Scaling Group

  • 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 ( highlighting key resources.

provider "aws" {
  region = "us-west-2" # Change this to your desired AWS region

resource "aws_vpc" "main" {
  cidr_block = "" # 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 = [""]

  vpc_id =

resource "aws_subnet" "public_subnet_1a" {
  availability_zone = "us-west-2a" # Change this to your desired AZ
  cidr_block        = "" # Change this to your desired CIDR block
  vpc_id            =

resource "aws_subnet" "public_subnet_1b" {
  availability_zone = "us-west-2b" # Change this to your desired AZ
  cidr_block        = "" # Change this to your desired CIDR block
  vpc_id            =

resource "aws_elb" "web_server_lb" {
  name               = "web-server-lb"
  availability_zones = ["us-west-1a", "us-west-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-008fe2fc65df48dac"
  instance_type = "t2.micro"
  user_data     = base64encode(<<-EOF
              echo "<html><body><h1>You're doing really Great</h1></body></html>" > /var/www/html/index.html
              nohup python -m SimpleHTTPServer 80 &

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          = []
  vpc_zone_identifier     = [,]
  launch_template {
    id =
  • Setting up the Environment

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

  • 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.🎯Task: 2

Test Scaling

Now, let's test the scalability of our Auto Scaling Group:

  • Access AWS Management Console: Navigate to the AWS Management Console and select the Auto Scaling Groups service.

  • Edit Auto Scaling Group Settings: Select the Auto Scaling Group you created and click on the "Edit" button. Adjust the "Desired Capacity" to 3 and save the changes.

  • Monitor EC2 Instances:Visit the EC2 Instances service and verify that new instances are launched to meet the increased capacity.

  • Scale Down:Decrease the "Desired Capacity" to 1 and wait for the extra instances to be terminated.

  • Verification: Check the EC2 Instances service again to confirm that the surplus instances have been successfully terminated.