Day 68 of #90daysofdevops
Hey Techies! Welcome to this blog
In this blog, we are going to start with Scaling with Terraform.
Understanding Scaling
Scaling continually monitors your applications to make sure that they are operating at your desired performance levels.
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 Create an Auto Scaling Group
Understanding the Terraform File
This blueprint that defines our scalable infrastructure. Below is a snippet of the Terraform file main.tf
highlighting key resources.
provider "aws"{
region = "ap-south-1"
}
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 = "ap-south-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 = "ap-south-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 = ["ap-south-1a", "ap-south-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-03f4878755434977f"
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
version = "$Latest"
}
}
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 -auto-approve
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.
Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Happy Learning!
~kritika :)
Connect with me: LinkedIn