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.
Create main.tf file as shown
provider "aws" {
region = "us-east-1" # Change the region as needed
}
resource "aws_iam_user" "example_user" {
name = "example-user"
}
resource "aws_iam_policy" "example_policy" {
name = "example-policy"
description = "Example Policy"
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::vk-new-bucket/*",
"Principal": "*"
}
]
})
}
resource "aws_iam_user_policy_attachment" "example_policy_attachment" {
user = aws_iam_user.example_user.name
policy_arn = aws_iam_policy.example_policy.arn
}
resource "aws_s3_bucket" "my_bucket" {
bucket = "vk-new-bucket"
acl = "public-read"
}
resource "aws_launch_configuration" "example_lc" {
name_prefix = "example-lc-"
image_id = "ami-005f9685cb30f234b"
instance_type = "t2.micro"
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" "example_asg" {
name = "example-asg"
launch_configuration = aws_launch_configuration.example_lc.name
min_size = 1
max_size = 3
desired_capacity = 2
health_check_type = "EC2"
# Use the provided public subnet and private subnet IDs
vpc_zone_identifier = ["subnet-03c407de02a28b252", "subnet-09548701ef59b66ac"]
}
Run terraform apply
command to create an auto-scaling group.
Goto EC2 dashboard, scroll down and click auto-scaling groups
Goto EC2 instance and check for running instances.
To increase the desired capacity of an Auto Scaling Group and verify instances:
Open the AWS Management Console and navigate to the Auto Scaling Groups service.
Select the Auto Scaling Group you want to modify and click the "Edit" button.
Increase the "Desired Capacity" to 3 and save your changes.
we can see 3 under the instance column.
Wait a few minutes for the new instances to be launched.
Go to the EC2 Instances service and confirm that the new instances have been successfully launched.
To decrease the desired capacity and verify instance termination:
Return to the Auto Scaling Groups service in the AWS Management Console.
Select the same Auto Scaling Group and click the "Edit" button.
Decrease the "Desired Capacity" to 1 and save your changes.
Now we can see 1 under the instance column.
Wait a few minutes for the extra instances to be terminated.
Go to the EC2 Instances service to confirm that the additional instances have been terminated.
At the last, we can see, that the 2 instances are terminated and only 1 instance is running...
Thank you so much for reading
Follow me on LinkedIn to see interesting posts like this : )