Hands-On Lab: Elastic Compute with EC2 Auto Scaling
Design high-performing and elastic compute solutions
Prerequisites
Before starting this lab, ensure you have the following ready:
- AWS Account: Administrator access or permissions to create EC2 instances, Launch Templates, and Auto Scaling Groups (ASGs).
- AWS CLI: Installed and configured with your credentials (
aws configure). - Infrastructure: A default VPC with at least two public subnets. You will need to note down a Subnet ID (e.g.,
subnet-12345678). - Prior Knowledge: Basic understanding of what an EC2 instance is and how to connect to one.
Learning Objectives
By completing this lab, you will be able to:
- Create an Amazon EC2 Launch Template to standardize instance deployments.
- Deploy an Auto Scaling Group (ASG) to manage elastic compute capacity.
- Configure a Target Tracking Scaling Policy based on CPU utilization.
- Artificially stress an EC2 instance to trigger an automated scale-out event.
Architecture Overview
This lab simulates an elastic compute environment. When the CPU load increases, CloudWatch detects the anomaly and triggers the Auto Scaling Group to launch additional instances to share the load.
Step-by-Step Instructions
Step 1: Create a Launch Template
A Launch Template acts as a blueprint for your EC2 instances. It defines the AMI, instance type, and configuration.
aws ec2 create-launch-template \
--launch-template-name brainybee-lt \
--version-description "v1" \
--launch-template-data '{"ImageId":"resolve:ssm:/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2","InstanceType":"t2.micro"}'📸 Screenshot: Terminal showing the JSON output with
"LaunchTemplateName": "brainybee-lt".
▶Console alternative
- Navigate to EC2 > Launch Templates in the AWS Management Console.
- Click Create launch template.
- Name it
brainybee-lt. - Under Application and OS Images, select Amazon Linux 2 AMI.
- Under Instance type, choose
t2.micro. - Click Create launch template.
[!TIP] We are using a special Systems Manager (SSM) parameter in the CLI to automatically fetch the latest Amazon Linux 2 AMI ID. This prevents errors from hardcoding outdated AMI IDs!
Step 2: Create the Auto Scaling Group
Now, we will create the ASG using the Launch Template. We will start with a minimum and desired capacity of 1, but allow it to scale up to 3.
Replace <YOUR_SUBNET_ID> with a valid subnet ID from your default VPC.
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name brainybee-asg \
--launch-template LaunchTemplateName=brainybee-lt,Version=1 \
--min-size 1 \
--max-size 3 \
--desired-capacity 1 \
--vpc-zone-identifier "<YOUR_SUBNET_ID>"📸 Screenshot: Terminal returning cleanly (this command provides no output on success).
▶Console alternative
- Navigate to EC2 > Auto Scaling Groups.
- Click Create Auto Scaling group.
- Name it
brainybee-asgand select thebrainybee-ltlaunch template. Click Next. - Select your Default VPC and choose at least two subnets. Click Next.
- Skip Load Balancing (click Next).
- Set Desired capacity: 1, Minimum: 1, Maximum: 3.
- Skip to Review and click Create.
Step 3: Configure a Target Tracking Scaling Policy
To make our compute elastic, we need a policy that scales out when the CPU works too hard.
aws autoscaling put-scaling-policy \
--auto-scaling-group-name brainybee-asg \
--policy-name cpu-tracking \
--policy-type TargetTrackingScaling \
--target-tracking-configuration '{"PredefinedMetricSpecification":{"PredefinedMetricType":"ASGAverageCPUUtilization"},"TargetValue":50.0}'📸 Screenshot: Terminal showing the configured policy ARN and CloudWatch alarms automatically created.
▶Console alternative
- Go to your
brainybee-asgin the Auto Scaling Groups console. - Click the Automatic scaling tab.
- Click Create dynamic scaling policy.
- Policy type: Target tracking scaling.
- Metric type: Average CPU utilization, Target value: 50.
- Click Create.
Step 4: Simulate Load to Trigger Auto Scaling
As noted in our architectural studies, if your web page is too small, it's hard to trigger scaling organically. We will "cheat" by running a busy-work loop directly on the server.
- Connect to your running EC2 instance (using EC2 Instance Connect via the Console).
- Run the following command to pin the CPU at 100%:
while true; do true; done[!NOTE] This loop will run endlessly and consume all available CPU cycles on a single core. Because
t2.microhas 1 vCPU, this will spike the instance's CPU utilization metric to 100% within a few minutes.
Checkpoints
Verify that your resources are running and that the scaling event occurred successfully.
Checkpoint 1: Verify Initial Instance
aws autoscaling describe-auto-scaling-groups \
--auto-scaling-group-names brainybee-asg \
--query "AutoScalingGroups[0].Instances[*].LifecycleState"Expected Result: You should see one instance listed as "InService".
Checkpoint 2: Verify Scaling Activity (After 3-5 minutes of running the loop)
aws autoscaling describe-scaling-activities \
--auto-scaling-group-name brainybee-asg \
--max-items 3Expected Result: You should see a recent activity with a description like "Launching a new EC2 instance..." due to the CPU alarm being triggered.
Teardown / Clean-Up
[!WARNING] Remember to run the teardown commands to avoid ongoing charges. Leftover EC2 instances will consume your free tier or incur hourly costs.
Execute the following commands to destroy all provisioned resources:
1. Scale the ASG down to 0 (to terminate instances immediately):
aws autoscaling update-auto-scaling-group \
--auto-scaling-group-name brainybee-asg \
--min-size 0 \
--desired-capacity 02. Delete the Auto Scaling Group:
aws autoscaling delete-auto-scaling-group \
--auto-scaling-group-name brainybee-asg \
--force-delete3. Delete the Launch Template:
aws ec2 delete-launch-template \
--launch-template-name brainybee-lt▶Console alternative
- Navigate to EC2 > Auto Scaling Groups. Select
brainybee-asgand click Delete. - Navigate to EC2 > Launch Templates. Select
brainybee-lt, click Actions, and select Delete template.
Troubleshooting
| Common Error / Issue | Cause | Fix |
|---|---|---|
| InvalidParameterValue error when creating ASG | The <YOUR_SUBNET_ID> provided does not exist or belongs to a different region. | Run aws ec2 describe-subnets and pick a valid Subnet ID from your current region. |
| Instances failing to launch | Your account might be blocked from using the t2.micro instance type due to regional capacity. | Modify the launch template to use t3.micro. |
| ASG doesn't scale out | It takes up to 5 minutes for the CloudWatch metric to register the CPU spike. | Keep the while true loop running and wait at least 5 minutes. Check CloudWatch Alarms in the console. |