Understanding Scaling
Scaling your infrastructure means adjusting the number of resources, like servers, to match the current demand. When your application gets more traffic, you'll need more resources to handle the load. When the traffic decreases, you can reduce the resources to save costs.
Terraform helps you manage this scaling process easily by letting you define your resources in a simple, declarative way. This means you describe what you want, and Terraform takes care of the rest.
In this article, we'll walk you through how to set up and test auto scaling with Terraform.
Step 1 : Create an Auto Scaling Group
An Auto Scaling Group (ASG) automatically adjusts the number of Amazon EC2 instances according to the conditions you define. Here’s how to set it up in Terraform.
1.1 Define a Launch Configuration
A Launch Configuration is like a blueprint for your Auto Scaling Group. It defines the configuration for the EC2 instances that will be created.
Open your main.tf
file and add the following code:
resource "aws_launch_configuration" "web_server_as" {
image_id = "ami-005f9685cb30f234b" # Specify the Amazon Machine Image (AMI) ID
instance_type = "t2.micro" # Specify the instance type
security_groups = [aws_security_group.web_server.name] # Link to the security group
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
}
image_id: This is the ID of the Amazon Machine Image (AMI) that your instances will use. AMIs are pre-configured operating system images.
instance_type: This specifies the type of EC2 instance, such as
t2.micro
, which is a small instance type suitable for low-traffic applications.security_groups: This specifies the security group that will control the inbound and outbound traffic for the instances.
user_data: This script runs when the instance starts. In this example, it creates a simple HTML file and starts a Python web server.
1.2 Define the Auto Scaling Group
The Auto Scaling Group uses the Launch Configuration to create and manage EC2 instances. Add this code to your main.tf
file:
resource "aws_autoscaling_group" "web_server_asg" {
name = "web-server-asg"
launch_configuration = aws_launch_configuration.web_server_as.name
min_size = 1 # Minimum number of instances
max_size = 3 # Maximum number of instances
desired_capacity = 2 # Desired number of instances
health_check_type = "EC2" # Health check type
load_balancers = [aws_elb.web_server_lb.name] # Link to load balancer
vpc_zone_identifier = [aws_subnet.public_subnet_1a.id, aws_subnet.public_subnet_1b.id] # VPC subnets
}
name: The name of the Auto Scaling Group.
launch_configuration: This links to the Launch Configuration defined earlier.
min_size: The minimum number of instances that should always be running.
max_size: The maximum number of instances that can be running.
desired_capacity: The number of instances that should be running under normal conditions.
health_check_type: This defines the type of health check to use (
EC2
in this case).load_balancers: This links to a load balancer, which distributes incoming traffic across the instances.
vpc_zone_identifier: These are the subnets in which the instances will be launched.
1.3 Apply the Configuration
Run the following command in your terminal to apply the configuration and create the Auto Scaling Group:
terraform apply
This command tells Terraform to create the resources you defined in the main.tf
file. Terraform will show you what changes it will make. Type yes
to confirm and apply the changes.
Step 2 : Test Scaling
Now that you have set up the Auto Scaling Group, let’s test it to ensure it scales correctly.
2.1 Increase the Desired Capacity
Go to the AWS Management Console.
Navigate to the Auto Scaling Groups service.
Select the Auto Scaling Group you created.
Click on the "Edit" button.
Change the "Desired Capacity" to 3.
Click "Save".
This tells the Auto Scaling Group to increase the number of running instances to 3.
2.2 Verify the New Instances
Wait a few minutes for the new instances to launch.
Navigate to the EC2 Instances service.
Verify that new instances have been created.
You should see that new instances have been launched to match the new desired capacity.
2.3 Decrease the Desired Capacity
Return to the Auto Scaling Groups service in the AWS Management Console.
Select your Auto Scaling Group and click "Edit".
Change the "Desired Capacity" to 1.
Click "Save".
This tells the Auto Scaling Group to decrease the number of running instances to 1.
2.4 Verify the Termination of Extra Instances
Wait a few minutes for the extra instances to be terminated.
Navigate to the EC2 Instances service.
Verify that the extra instances have been terminated.
You should see that the number of running instances has decreased to match the new desired capacity.
Conclusion
Congratulations! You have successfully scaled your infrastructure with Terraform. By setting up an Auto Scaling Group, you now have a dynamic system that can adjust resources according to the demands of your application, ensuring optimal performance and cost-efficiency.
Terraform's declarative approach makes it easy to manage your infrastructure and adapt to changing needs. Keep exploring Terraform to discover more ways to automate and optimize your cloud resources.
Happy Learning! 😊