DevOps, Day - 68

DevOps, Day - 68

Scaling with Terraform πŸš€

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

πŸ’‘
This configuration will create and manage instances as part of the Auto Scaling Group, ensuring the desired number of instances are launched and maintained.

Goto EC2 instance and check for running instances.


To increase the desired capacity of an Auto Scaling Group and verify instances:

  1. Open the AWS Management Console and navigate to the Auto Scaling Groups service.

  2. Select the Auto Scaling Group you want to modify and click the "Edit" button.

  3. Increase the "Desired Capacity" to 3 and save your changes.

  4. we can see 3 under the instance column.

  5. Wait a few minutes for the new instances to be launched.

  6. 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:

  1. Return to the Auto Scaling Groups service in the AWS Management Console.

  2. Select the same Auto Scaling Group and click the "Edit" button.

  3. Decrease the "Desired Capacity" to 1 and save your changes.

  4. Now we can see 1 under the instance column.

  5. Wait a few minutes for the extra instances to be terminated.

  6. 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 : )

Linkedin

Β