Scaling with Terraform ๐Ÿš€

Scaling with Terraform ๐Ÿš€

ยท

3 min read

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:

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-005f9685cb30f234b"
  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_lc.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]
}

This Terraform script generates two AWS components: an autoscaling group and a launch configuration. The launch configuration defines parameters such as image ID, instance type, security group, and user data for initiating new instances. The user data script, upon instance launch, installs a basic web server and presents a straightforward HTML page conveying the message "You're doing exceptionally well."

Regarding the AWS resources:

  1. aws_launch_configuration: This section establishes a launch configuration tailored for EC2 instances set to be deployed within our autoscaling group. It mandates the following arguments:

    • image_id: The ID of the EC2 image to launch.

    • instance_type: The type of instance to launch.

    • security_groups: An array of associated security group IDs.

  2. aws_autoscaling_group: This segment introduces an Auto Scaling Group resource, specifying parameters such as:

    • max_size: The maximum size allowed for the Auto Scaling Group.

    • min_size: The minimum size required for the Auto Scaling Group.

    • desired_capacity: The desired number of Amazon EC2 instances to maintain within the group.

This Terraform script sets up an AWS VPC with two public subnets in different availability zones. It includes an internet gateway, a public route table, and route table associations. The script also creates an AWS security group that permits SSH and HTTP traffic. Additionally, it defines a launch configuration that uses user data to start a Python HTTP server on port 80. Finally, it establishes an autoscaling group with a minimum of 1 instance, a maximum of 3 instances, and a desired capacity of 2 instances, spread across the two subnets.

To get started, run terraform init to initialize the Terraform project.

After that, run terraform plan to review the execution plan,

and execute terraform apply to create the Auto Scaling Group.

After these steps, two new EC2 instances will be created, as the desired capacity is set to 2.

The launch configuration is successfully created, and the auto-scaling group is now up and running with two instances.

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 1 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.


Thank you for reading until here. See you in the next one.

ย