Open In App

Creating Autoscaling And Autoscaling Group Using Terraform

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In modern cloud computing environments, the ability to dynamically scale resources in light of changing interests is essential for keeping up with execution, accessibility, and cost-effectiveness. Autoscaling and Autoscaling Groups are key parts that enable this unique scaling functionality in cloud infrastructures. With the assistance of Terraform, an Infrastructure as Code (IaC) tool, provisioning and overseeing Autoscaling Groups becomes smoothed out and automated.

This article explores the most common way of making Autoscaling Groups using Terraform, covering fundamental ideas, configurations, and best practices. By utilizing Terraforms’ declarative syntax and infrastructure management capacities, clients can characterize and send Autoscaling Groups easily, ensuring a scalable and strong cloud infrastructure.

Understanding Of Primary Terminologies

Autoscaling

Autoscaling is a dynamic and mechanized distributed computing highlight intended to adjust to fluctuating jobs. Envision you’re running a web application, and the quantity of clients getting to it differs over the course of the day. Autoscaling permits your infrastructure to change the quantity of assets, for example, virtual machines or occurrences, in light of interest naturally. At the point when traffic increments, autoscaling adds more assets to proficiently deal with the heap. On the other hand, during times of low interest, it lessens the quantity of assets to save costs. This automated interaction guarantees that your application keeps up with ideal execution and responsiveness without manual mediation.

Auto Scaling Group (ASG)

Auto Scaling Group (ASGs) are at the center of the autoscaling process. Picture an ASG as a chief supervising a gathering of indistinguishable laborers (occurrences). ASGs characterize the ideal number of occurrences you need to keep up with, alongside the base and most extreme cutoff points. For instance, you could constantly need somewhere around two occurrences running however permit up to five when traffic surges. ASGs consequently change the gathering size in view of characterized scaling approaches. Also, ASGs can disseminate occurrences across various areas for expanded unwavering quality. By partner ASGs with load balancers, the responsibility is productively dispersed, ensuring a decent and responsive framework as your application scales.

Terraform:

Terraform is an open-source Infrastructure as Code (IaC) tool created by HashiCorp. It permits users to define, provision, and oversee foundation assets utilizing decisive setup documents. With Terraform, framework is portrayed in a comprehensible language, and the device handles the coordination and sending of assets across different cloud suppliers and on-premises conditions. Terraform upgrades computerization, joint effort, and repeatability in Infrastructure the board processes.

Step-By-Step Process To Create Autoscaling And Autoscaling Group Using Terraform

Step 1: Setting Up AWS account

  • Go to AWS Management Console
  • Login with by using your credentials
  • Now you need to generate access key to authenticate Terraform with your AWS account
  • In AWS management console in home screen search for IAM ( Identity and Access Management ) service. Choose Users and click on Add user.
  • Give a username and select administration access as the access type. Attach necessary permissions to the user.
  • Review the user details and create the user. Now you will see the access key ID and secret access key. Save this information securely as it will be required when configure Terraform.

Step 2: Launch EC2 instance And Install Terraform

  • Launch EC2 instance with Amazon Linux2 Kernel 5.10(AMI) along with port numbers set SSH – 22 AND HTTP and select storage t2.micro

Launching EC2 Instance

  • Now connect with git bash by using SSH command or use any type of terminal like putty, Powershell e.t.c..

Remote login in of  Instance

  • Now install terraform by using following commands
sudo yum install -y yum-utils shadow-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform


yum-util

  • The following screenshot illustrates on installation of terraform:

Installation of terraform

Step 3: Terraform Script

  • Now create a file by using touch command with .tf extension. In that file now we are writing entire infrastructure script.
# Configure AWS provider
provider "aws" {
region = "us-east-1" # Change this to your preferred AWS region
}


Creating AWS Provider

AWS Launch Configuration

  • This section defines an AWS launch configuration. It specifies the instance details, including the type (t2.micro), key pair name for SSH access, and the Amazon Machine Image (AMI) ID for the operating system.
  • User data is also provided, allowing you to run scripts during instance initialization. The lifecycle block ensures that the new launch configuration is created before the old one is destroyed.
# Define launch configuration
resource "aws_launch_configuration" "example" {
name = "example-launch-config"

# Define instance details
instance_type = "t2.micro"
key_name = "WORDPRESS" # Replace with your EC2 key pair name
image_id = "ami-07761f3ae34c4478d" # Replace with your desired AMI ID

# Include any additional configurations needed
user_data = <<-EOF
#!/bin/bash
echo "Hello, this is user data!" > /tmp/user_data_output.txt
EOF

lifecycle {
create_before_destroy = true
}
}



  • Creating Subnets with the following code:
resource "aws_subnet" "publicsubnet1" {
vpc_id = aws_vpc.VPC-1.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
availability_zone = "us-east-1b"
tags = {
Name = "SUBNET"
}
}





Define Autoscaling Group

  • This part defines the Autoscaling Group. It specifies the desired capacity (initial number of instances), maximum and minimum sizes for scaling, and the VPC subnet identifiers where instances will be launched
# Define Autoscaling Group
resource "aws_autoscaling_group" "example" {
desired_capacity = 2
max_size = 5
min_size = 1
vpc_zone_identifier = ["public-subnet1", "public-subnet2"] # Replace with your subnet IDs
# Reference the launch configuration
launch_configuration = aws_launch_configuration.example.id
# Attach policies for scaling based on conditions (e.g., CPU utilization)
health_check_type = "EC2"
health_check_grace_period = 300
force_delete = true
}





8

Define Scaling Policies

  • Here, two scaling policies are defined using the aws_autoscaling_policy resource. One policy (scale_up_policy) increases the number of instances by 1 when triggered, and the other (scale_down_policy) decreases the number by 1.
  • The cooldown parameter adds a delay before additional scaling actions can occur. Both policies are linked to the Autoscaling Group.
resource "aws_autoscaling_policy" "scale_up_policy" {
name = "scale-up-policy"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 300
estimated_instance_warmup = 180
# Link to the Autoscaling Group
autoscaling_group_name = aws_autoscaling_group.example.name
}
resource "aws_autoscaling_policy" "scale_down_policy" {
name = "scale-down-policy"
scaling_adjustment = -1
adjustment_type = "ChangeInCapacity"
cooldown = 300
# Link to the Autoscaling Group
autoscaling_group_name = aws_autoscaling_group.example.name
}


Output Block

  • Finally, an output block is defined to display details of the created Autoscaling Group when the Terraform script is executed.
# Output the Autoscaling Group details
output "autoscaling_group_details" {
value = aws_autoscaling_group.example
}


AutoScaling Groups Definitions

Step 4: Now execute terraform execution commands.

terraform init


terraform initialization

  • Now execute following commands
terraform fmt
terraform validate
terraform plan


terraform plan and validation

  • Now execute main command
terraform apply --auto-approve


  • When we execute this command then automatically our infrastructure was build automatically

terraform apply with autoapprove

  • We created the autoscaling and autoscaling group resources successfully. Here the outcomes of those. The below screenshot illustrates the resource creation.

Resource Creation

  • The following screenshots that we have created the autoscaling groups successfully.

Auto scaling groups created

Conclusion

All in all, Autoscaling and Autoscaling groups are essential parts in distributed computing conditions, considering the programmed change of assets in light of evolving requests. Terraform, a Infrastructure as Code (IaC) tool, works on the provisioning and the board of these assets by giving a revelatory way to deal with characterizing infrastructure configurations.

The capacity to powerfully scale assets guarantees ideal execution, accessibility, and cost-adequacy, making Autoscaling and Autoscaling Gatherings fundamental for current cloud-based applications and services. With Terraform, foundation the board turns out to be more effective, versatile, and repeatable, enabling associations to assemble and keep up with vigorous cloud conditions easily.

AutoScaling And Autoscaling Groups – FAQs

Might I At Any Point Involve Terraform For Autoscaling In Different Cloud Providers?

Yes, Terraform is cloud-agnostic and supports different providers. Change the supplier design in your Terraform script accordingly.

How Does Autoscaling Deal With Examples End During Downsize?

Autoscaling effortlessly terminates instances utilizing a characterized end policy. It considers factors like case wellbeing, burden, and accessibility zones.

Might I At Any Point Modify The Scaling Approaches For Explicit Occurrences Inside An Autoscaling Gathering?

Yes, you can define policies based on specific circumstances (e.g., CPU utilization, custom measurements) to scale instances inside the group.

Is It Conceivable To Set Up Warning Alarms When Autoscaling Events Happen?

Yes, you can design Autoscaling warnings utilizing cloud provider services(e.g., AWS CloudWatch Alerts) related to your Auto Scaling Group.

Could I At Any Point Manually Change The Size Of An Autoscaling Group Made Utilizing Terraform?

Yes, you can physically set the ideal limit or update the design in the Terraform script and apply the changes to change the Group size.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads