Day 68- 90DaysOfDevOps
Scaling With Terraform

Hey Learners! Welcome back. In the few last challenges, we launched our first static web page on the Apache server in a new VPC and created and managed S3 Buckets using Terraform. Moving forward we'll scale our infrastructure. We all know how to scale manually using AWS console but in this challenge, we'll do it with the help of Terraform. Let's scale it...
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.

To know more about Auto Scaling Group in AWS and how to create manually by AWS console check out the DAY40 challenge.
Pre-requisite:- Read and understand the Day40 challenge so that you'll get a clear picture of ASG.
Steps:-
Create a Security Group
Create a Template for an EC2 Auto Scaling Group with user_data
Create Auto-Scaling Group
Apply the configuration
Validate
1- Create a Security Group
Create a Security Group as shown below
provider "aws" {
region = "ap-south-1"
}
resource "aws_security_group" "MySG" {
name_prefix = "SG-ASG"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
2- Create a Template for an EC2 Auto Scaling Group with user_data
For EC2 Auto Scaling Group we have to create Launch Configuration which is an instance configuration template that an Auto Scaling Group uses to launch EC2 instances.
In Terraform we create configuration files as shown below for Templates for an EC2 Auto Scaling Group
resource "aws_launch_configuration" "ASG" {
name_prefix = "testingASG"
image_id = "ami-0f5ee92e2d63afc18"
instance_type = "t2.micro"
security_groups = [aws_security_group.MySG.id]
user_data = <<-EOF
#!/bin/bash
sudo apt-get update -y
sudo apt-get install nginx -y
sudo systemctl start nginx
echo "Welcome to my website created by Terraform!" > /var/www/html/index.html
sudo systemctl restart nginx
sudo systemctl enable nginx
EOF
}
3- Create Auto-Scaling Group(ASG)
Create an ASG as shown below
resource "aws_autoscaling_group" "testgrp" {
name = "testgrp"
launch_configuration = aws_launch_configuration.ASG.name
min_size = 1
max_size = 3
desired_capacity = 2
health_check_type = "EC2"
vpc_zone_identifier = ["subnet-057f98bc012758e0d"] #replace subnet as per your VPC
}
The whole main.tf will look like
provider "aws" {
region = "ap-south-1"
}
resource "aws_security_group" "MySG" {
name_prefix = "SG-ASG"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_launch_configuration" "ASG" {
name_prefix = "testingASG"
image_id = "ami-0f5ee92e2d63afc18"
instance_type = "t2.micro"
security_groups = [aws_security_group.MySG.id]
user_data = <<-EOF
#!/bin/bash
sudo apt-get update -y
sudo apt-get install nginx -y
sudo systemctl start nginx
echo "Welcome to my website created by Terraform!" > /var/www/html/index.html
sudo systemctl restart nginx
sudo systemctl enable nginx
EOF
}
resource "aws_autoscaling_group" "testgrp" {
name = "testgrp"
launch_configuration = aws_launch_configuration.ASG.name
min_size = 1
max_size = 3
desired_capacity = 2
health_check_type = "EC2"
vpc_zone_identifier = ["subnet-057f98bc012758e0d"] #replace subnet as per your VPC
}
4- Apply the configuration
Use the terraform apply command to apply the changes

5- Validate
Go to the AWS console. Choose Auto Scaling Group from the left navigation bar in the EC2 service. Check the details for ASG.

Choose EC2 service and make sure 2 new instances are added. Copy the PublicIP of both and check whether a web page is hosted or not as per user_data.

Check for the second instance.

We successfully created ASG. Let's test ASG.
Task 2- Test Scaling
Go to the AWS console. Select Auto Scaling Group from the left navigation pane. Select ASG as listed. Edit the Group details. Make the Desired capacity as 3 and save the changes.

Go to Instances and check that one new (3rd one) instance getting initialized.

Access the web page by hitting the 3rd (recently) created instance's public IP on the browser.

If you choose desired capacity as 1 then the other two instances get terminated.

Check instances

Instead of doing it manually by AWS console, you can make changes in the main.tf then validate the changes.
Don't forget to destroy provisioned infrastructure using the terraform destroy command.

Thank you so much for taking the time to read till the end! Hope you found this blog informative.
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.
Find me on:- Hashnode LinkedIn Github
Happy Learning!




