Table of contents
Dear Learners, In the previous article we discussed the S3 Bucket Configuration and Management, In today's article we will discuss the Auto Scaling with Terraform.
TABLE OF CONTENTS:
Understanding Scaling
Task-01
Create an Auto Scaling Group
Task-02
Test Scaling
Understanding the Auto 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 the more resource to handle the increased load. And as the load decreases,you can remove the extra resources to save costs.
Task-01
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 yourmain.tffile add the following syntax 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]
}
The Terraform code creates 2 AWS resources an autoscaling group and a launch configuration.
The launch configuration specifies the image ID, instance type, security group, and user data to be used when launching new instances.
The autoscaling group defines the parameters for the group, including the launch configuration to use, minimum and maximum number of instances and desired capacity.
aws_launch_configuration:- This resource creates a launch configuration for EC2 instances that we are going to deploy as part of our autoscaling group.
The Following arguments are required:
image_id - The EC2 image ID to launch.
instance_type - The size of instance to launch.
security_groups - A list of associated security groups IDS.
AWS_autoscaling_group - Provides an Auto Scaling Group resource.
max_size - Maximum size of the Auto Scaling Group.
min_size - MinimunNm size of the Auto Scaling Group.
desired_capacity - Number of Amazon EC2 instance that should be running in the group.
terraform main.tf configuration file Diagram
terraform main.tf configuration file Diagram 1
terraform main.tf configuration file Diagram 2
terraform main.tf configuration file Diagram 3
The Terraform script creates an AWS VPC with 2 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 data to start a python HTTP server on port 80 and an autoscaling group with a minimum of 1 maximum of 3 and desired capacity of 2 instances spread across the 2 subnets.
Run terraform init to initialize the Terraform project.
Run terraform init to initialize the Terraform project Diagram.
Run the Terraform plan
Run the terraform apply to create the Auto scaling group
Run the terraform apply to create the Auto scaling group Diagram
Run the terraform apply to create the Auto scaling group Diagram 1
Run the terraform apply to create the Auto scaling group Diagram 2
Below 2 instances created as desired capacity set to 2
Below 2 instances created as desired capacity set to 2 Diagram.
Launch configuration successfully created.
Launch configuration successfully created Diagram.
Auto scaling group is created. Auto scaling group details.
Auto scaling group is created. Auto scaling group details Diagram.
Two instances are running in auto scaling group.
Two instances are running in auto scaling group Diagram.
Task 2: Test Autoscaling.
Go to the AWS Management console and select the Auto scaling Groups.
Select the Auto Scaling Group you just created and click on the Edit Button.
Select the Auto Scaling Group you just created and click on the Edit Button Diagram.
Increase the Desired Capacity to 3 and click on the save button.
Increase the Desired Capacity to 3 and click on the save button Diagram.
Wait a few minutes for the new instance to be launched.
Go to the EC2 instances service and verify that the new instance have been launched which are highlight with Blue tick
Go to the EC2 instances service and verify that the new instance have been launched Diagram.
Decrease the Desired capacity to 1 and wait a few minutes for the extra instances to be terminated.
Decrease the Desired capacity to 1 and wait a few minutes for the extra instances to be terminated Diagram
Go to the EC2 instances service and verify that the extra instance have been terminated which are shutting Down.
Go to the EC2 instances service and verify that the extra instance have been terminated.
Thank you for reading!! Hope you find this helpful.
#day68challenge#90daysofdevops
Always open to suggestions..!!
~ Manoj Bhamidipati ๐