Understanding Scaling:
Auto Scaling in AWS is a feature that allows you to automatically adjust the capacity of your Amazon Web Services (AWS) resources based on the demand for your application or service. It is particularly useful for managing workloads that experience fluctuations in traffic and need to scale up or down dynamically.
With auto-scaling, you can define scaling policies that specify the conditions under which your resources should be scaled. These policies can be based on various metrics, such as CPU utilization, network traffic, or application-specific metrics. When the specified conditions are met, auto-scaling automatically adds or removes instances from your resource group accordingly.
💡Auto Scaling works with several AWS services, including Amazon EC2 (Elastic Compute Cloud) instances, Amazon ECS (Elastic Container Service) tasks, and Amazon DynamoDB tables. It monitors the health of your resources and replaces any unhealthy instances to maintain the desired level of performance and availability.
The benefits of using auto-scaling in AWS include:
Auto-scaling automatically replaces unhealthy instances, reducing the risk of downtime.
Auto-scaling allows you to scale your resources up and down based on demand. This helps optimize costs by ensuring you only pay for the resources you actually need.
With auto-scaling, you can maintain a consistent level of performance even during high-demand periods by automatically adding resources.
Auto-scaling automates the process of scaling your resources, eliminating the need for manual intervention and reducing operational overhead.
How does AWS auto-scaling work?
AWS Auto Scaling works by automatically adjusting the number of EC2 instances or other AWS resources in response to changing demand or resource utilization. Here's how it typically works:
Auto Scaling Groups (ASGs): You start by creating an auto-scaling group, which defines the set of EC2 instances or other resources that will be automatically scaled. You specify the minimum and maximum number of instances, launch configuration, and other parameters.
Scaling Policies: Auto Scaling allows you to define scaling policies that determine when and how to scale your resources. There are two types of scaling policies:
- Target Tracking Scaling: This policy maintains a specific metric (such as CPU utilization or request count) at a target value. Auto-scaling adjusts the number of instances to keep the metric at or near the target.
Health Checks: Auto Scaling continuously monitors the health of instances in your Auto Scaling group. If an instance becomes unhealthy (e.g., due to failed system status checks or a failed ELB health check), Auto Scaling terminates it and launches a new instance to replace it.
Scaling Events: When a scaling event occurs (such as adding or removing instances), Auto Scaling triggers actions based on the scaling policies you've defined. These actions can include launching new instances, terminating instances, or notifying you via Amazon SNS.
Integration with Other AWS Services: Auto Scaling integrates with other AWS services such as Elastic Load Balancing (ELB) and Amazon CloudWatch. ELB distributes incoming traffic across instances in your auto-scaling group, while CloudWatch provides monitoring and metrics for auto-scaling, allowing you to define alarms and trigger scaling events based on custom metrics.
Why do we use auto-scaling with Terraform?
Auto-scaling and Terraform are often used together to provide a robust and scalable infrastructure solution. Here's why:
✔Auto-scaling can handle traffic spikes without manual intervention, ensuring your application remains responsive and available
✔Auto-scaling can help optimize costs by automatically scaling up or down based on demand. When traffic is low, fewer resources are used, reducing costs.
✔By using auto-scaling groups across multiple Availability zones (AZs), you can ensure the high availability of your application. If one AZ experiences an issue, Auto Scaling can launch instances in another AZ to maintain application availability.
✔Terraform provides consistency across environments by defining infrastructure in a declarative manner. When combined with auto-scaling, it automates the provisioning and scaling of resources, reducing the potential for human error and ensuring that environments are consistent and reproducible.
✔Terraform has built-in support for managing auto-scaling groups, enabling you to define scaling policies, set up health checks, and manage other configurations as part of your infrastructure code.
Task 1:
Create an Auto-scaling group.
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:
Step 1: 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"
vpc_zone_identifier = [aws_subnet.public_subnet_1a.id, aws_subnet.public_subnet_1b.id]
}
This Terraform code creates two AWS resources: an auto-scaling group and a launch configuration.
🌀The launch configuration specifies the image ID, instance type, security group, and user data to be used when launching new instances. The user data script installs a simple web server and serves a basic HTML page with the message "You're doing really great" when an instance is launched.
🌀The auto-scaling group defines the parameters for the group, including the launch configuration to use, minimum and maximum number of instances, and desired capacity.
🌀 This resource creates a launch configuration for EC2 instances that we are going to deploy as part of our auto-scaling group.
⌛This Terraform script creates an AWS VPC with two public subnets in different availability zones, an internet gateway, a public route table, and route table associations.
⌛It also creates an AWS security group that allows SSH and HTTP traffic, a launch configuration that uses user data to start a Python HTTP server on port 80, and an auto-scaling group with a minimum of 1, maximum of 3, and desired capacity of 2 instances, spread across the two subnets.
Step 2: Run terraform init
command to start the Terraform project.
Step 3: Run terraform plan
command
Step 3: Execute terraform apply
command
Launch configuration is successfully created, and an auto-scaling group is also created.
Task 2:
Test Scaling
Step 1: Navigate to the AWS Management Console and choose the Auto Scaling Groups service.
Step 2: Locate the Auto Scaling Group you just created and select the "Edit" button.
Step 3: Modify the "Desired Capacity" to 3, then click the "Save" button.
Step 4: Wait a few minutes for the new instances to be launched.
Step 5: Go to the EC2 Instances service and verify that the new instances have been launched.
Step 6: Reduce the "Desired Capacity" to 1 and wait a few minutes for the remaining instances to be terminated.
Step 7: Go to the EC2 Instances service and verify that the extra instances have been terminated.
Overall, AWS Auto Scaling simplifies the process of managing and scaling your infrastructure by automatically adjusting resources based on demand or resource utilization, ensuring that your applications remain responsive and available while optimizing costs.
Thank you for 📖reading my blog. 👍 Like it and share it 🔄 with your friends . Hope you find it helpful🤞
Happy learning😊😊