Day 68 - Scaling with Terraform ๐Ÿš€

Nilkanth MistryNilkanth Mistry
4 min read

Welcome to Day 68 of the 90DaysOfDevOpsChallenge! Yesterday, we learned how to create an AWS S3 Bucket with Terraform. Today, we will explore 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 need more resources to handle the increased load. When the load decreases, you can remove the extra resources to save costs. ๐Ÿ’ธ

Terraform simplifies scaling by providing a declarative way to define your resources. ๐Ÿ“ You specify the number of resources you need, and Terraform handles the creation or destruction of those resources. Let's dive into scaling with Terraform! ๐Ÿš€

  • Install Terraform on your local machine.
sudo apt-get update
sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update
sudo apt-get install terraform
terraform -version

  1. Install AWS CLI:

     sudo apt-get update
     sudo snap install aws-cli --classic
     aws --version
     aws configure
    

    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:

    ๐Ÿ“ 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-02bf8ce06a8ed6092"
       instance_type   = "t2.micro"
       security_groups = [aws_security_group.web_server.name]
    
       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
     }
    
     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_2a.id, aws_subnet.public_subnet_2b.id]
     }
    

    ๐Ÿ› ๏ธ This Terraform code creates two AWS resources: an Auto Scaling Group and a launch configuration.

    • aws_launch_configuration: This resource creates a launch configuration for EC2 instances that we are going to deploy as part of our Auto Scaling Group.

      • image_id: The EC2 image ID to launch. ๐Ÿ–ผ๏ธ

      • instance_type: The size of instance to launch. ๐Ÿ“

      • security_groups: A list of associated security group IDs. ๐Ÿ›ก๏ธ

      • user_data: The script to be executed when an instance is launched, which starts a simple web server. ๐ŸŒ

    • aws_autoscaling_group: Provides an Auto Scaling Group resource.

      • max_size: Maximum size of the Auto Scaling Group. ๐Ÿš€

      • min_size: Minimum size of the Auto Scaling Group. ๐Ÿ“‰

      • desired_capacity: Number of Amazon EC2 instances that should be running in the group. ๐Ÿ“ˆ

๐Ÿ”ง This Terraform script creates an AWS VPC with two public subnets in different availability zones, an internet gateway, a public route table, and route table associations. It also creates an AWS security group that allows SSH and HTTP traffic, a launch configuration that uses user data to start a Python HTTP server on port 80, and an Auto Scaling Group with a minimum of 1, maximum of 3, and desired capacity of 2 instances, spread across the two subnets.

  1. Run terraform init to initialize the Terraform project. โš™๏ธ

  2. Run terraform plan to see the execution plan. ๐Ÿ“

  3. Run terraform apply to create the Auto Scaling Group. ๐Ÿš€

     terraform apply
    

๐Ÿ” Below, two new EC2 instances created as desired capacity set to 2:

โœ… Launch configuration successfully created:

๐Ÿ“Š Auto Scaling Group is created with group details:

๐Ÿš€ Two instances are running in the Auto Scaling Group:

Task 2: Test Scaling ๐Ÿ“ˆ๐Ÿ“‰

  1. Go to the AWS Management Console and select the Auto Scaling Groups service. ๐ŸŒ

  2. Select the Auto Scaling Group you just created and click on the "Edit" button. โœ๏ธ

  3. Increase the "Desired Capacity" to 3 and click on the "Save" button. ๐Ÿ’พ

  4. Wait a few minutes for the new instances to be launched. โณ

  5. Go to the EC2 Instances service and verify that the new instances have been launched. ๐Ÿ–ฅ๏ธโœ…

  6. Decrease the "Desired Capacity" to 1 and wait a few minutes for the extra instances to be terminated. โณ

  7. Go to the EC2 Instances service and verify that the extra instances have been terminated. ๐Ÿ–ฅ๏ธโŒ

๐ŸŽ‰ Congratulations! You have successfully scaled your infrastructure with Terraform. Keep up the great work and happy learning! ๐Ÿ“š๐Ÿš€

Happy Learning! ๐Ÿ˜Š

Keep experimenting and learning. Tomorrow, we'll explore more exciting topics in our DevOps journey. See you then! ๐Ÿ‘‹

0
Subscribe to my newsletter

Read articles from Nilkanth Mistry directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nilkanth Mistry
Nilkanth Mistry

Embark on a 90-day DevOps journey with me as we tackle challenges, unravel complexities, and conquer the world of seamless software delivery. Join my Hashnode blog series where we'll explore hands-on DevOps scenarios, troubleshooting real-world issues, and mastering the art of efficient deployment. Let's embrace the challenges and elevate our DevOps expertise together! #DevOpsChallenges #HandsOnLearning #ContinuousImprovement