DevOps(Day-95): Auto Scaling with Terraform

 

Yesterday, we learned how to AWS S3 Bucket with Terraform. Today, we will see how to scale our infrastructure with Terraform.

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.

  1. Create a terraform.tf file declaring the providers to be installed in the system according to the resources required.

  2. Create a provider.tf file to declare the region.

  3. Create a vpc.tf file to create VPC for the instance.

  4. Create two public subnets that need to be further initialised to the auto-scaling group as a multi-subnet initialization. Therefore create a subnet.tf terraform file.

  5. Create internetgateway.tf file and declare the configurations.

  6. Create routetable.tf file to define all the configurations for routing.

  7. Create a security group to define and allow SSH, HTTP and egress traffic to the instance. Therefore create a securitygroup.tf file

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 your main.tf file, add the following code 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"
  load_balancers       = [aws_elb.web_server_lb.name]
  vpc_zone_identifier  = [aws_subnet.public_subnet_1a.id, aws_subnet.public_subnet_1b.id]
}
  • Create a ec2_autoscalling.tf file while contains EC2 configuration along with auto-scaling configurations.

  • Run terraform apply to create the Auto Scaling Group and all of its dependent resources through terraform as mentioned in the pre-requisites.

  • Let's navigate to AWS management console to check the created auto-scaling group.

  • In the auto-scaling terraform configuration we set the desired capacity to 2. Therefore there must be two instances created.

  • Go to the AWS Management Console and select the Auto Scaling Groups service.

  • Select the Auto Scaling Group you just created and click on the "Edit" button.

  • Increase the "Desired Capacity" to 3 and click on the "Save" button.

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

  • Go to the EC2 Instances service and verify that the new instances have been launched.

  • Decrease the "Desired Capacity" to 1 and wait a few minutes for the extra instances to be terminated.

  • Go to the EC2 Instances service and verify that the extra instances have been terminated.

Thanks for reading my article. Have a nice day.

Comments

Popular posts from this blog

DevOps (Day-1) Introduction

DevOps(Day-3) - File Management in Linux

DevOps(Day-2) Linux