Scaling with Terraform
Scaling involves the dynamic adjustment of resources to align with the evolving requirements of your application. As your application expands, the demand for resources grows, necessitating the addition of more capacity. Conversely, during periods of reduced demand, surplus resources can be efficiently removed to optimize costs.
Terraform simplifies the process of scaling your infrastructure by offering a declarative approach to resource definition. You can precisely specify the desired quantity of resources, and Terraform will intelligently manage the creation or removal of resources based on the defined parameters.
Task 1: Create an Auto Scaling Group
Auto Scaling Groups are employed to automatically adjust the number of EC2 instances in response to current demand. To establish an Auto Scaling Group, adhere to these steps:
Lets update main.tf file by incorporating the following code to instantiate an Auto Scaling Group
This Terraform script generates two AWS components: an autoscaling group and a launch configuration. The launch configuration defines parameters such as image ID, instance type, security group, and user data for initiating new instances. The user data script, upon instance launch, installs a basic web server and presents a straightforward HTML page conveying the message "You're doing exceptionally well."
Lets trigger terraform script and check .
Post Terraform Apply we are able to see resource got created.
Task 2: Test Scaling
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.
*updated desired capacity to 3.
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.
We could see 3 new instance are available in console.
Decrease the "Desired Capacity" to 1 and wait a few minutes for the extra instances to be terminated.
*updated desired capacity to 1.
Go to the EC2 Instances service and verify that the extra instances have been terminated.
We could see 2 instance got terminated and just 1 active instance which was our desired capacity.
Congratulations🎊🎉 we have successfully scaled our infrastructure with Terraform.