Hands-On Lab870 words

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:

  1. Create an Amazon EC2 Launch Template to standardize instance deployments.
  2. Deploy an Auto Scaling Group (ASG) to manage elastic compute capacity.
  3. Configure a Target Tracking Scaling Policy based on CPU utilization.
  4. 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.

Loading Diagram...

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.

bash
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
  1. Navigate to EC2 > Launch Templates in the AWS Management Console.
  2. Click Create launch template.
  3. Name it brainybee-lt.
  4. Under Application and OS Images, select Amazon Linux 2 AMI.
  5. Under Instance type, choose t2.micro.
  6. 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.

bash
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
  1. Navigate to EC2 > Auto Scaling Groups.
  2. Click Create Auto Scaling group.
  3. Name it brainybee-asg and select the brainybee-lt launch template. Click Next.
  4. Select your Default VPC and choose at least two subnets. Click Next.
  5. Skip Load Balancing (click Next).
  6. Set Desired capacity: 1, Minimum: 1, Maximum: 3.
  7. 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.

bash
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
  1. Go to your brainybee-asg in the Auto Scaling Groups console.
  2. Click the Automatic scaling tab.
  3. Click Create dynamic scaling policy.
  4. Policy type: Target tracking scaling.
  5. Metric type: Average CPU utilization, Target value: 50.
  6. 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.

  1. Connect to your running EC2 instance (using EC2 Instance Connect via the Console).
  2. Run the following command to pin the CPU at 100%:
bash
while true; do true; done

[!NOTE] This loop will run endlessly and consume all available CPU cycles on a single core. Because t2.micro has 1 vCPU, this will spike the instance's CPU utilization metric to 100% within a few minutes.

Loading Diagram...

Checkpoints

Verify that your resources are running and that the scaling event occurred successfully.

Checkpoint 1: Verify Initial Instance

bash
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)

bash
aws autoscaling describe-scaling-activities \ --auto-scaling-group-name brainybee-asg \ --max-items 3

Expected 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):

bash
aws autoscaling update-auto-scaling-group \ --auto-scaling-group-name brainybee-asg \ --min-size 0 \ --desired-capacity 0

2. Delete the Auto Scaling Group:

bash
aws autoscaling delete-auto-scaling-group \ --auto-scaling-group-name brainybee-asg \ --force-delete

3. Delete the Launch Template:

bash
aws ec2 delete-launch-template \ --launch-template-name brainybee-lt
Console alternative
  1. Navigate to EC2 > Auto Scaling Groups. Select brainybee-asg and click Delete.
  2. Navigate to EC2 > Launch Templates. Select brainybee-lt, click Actions, and select Delete template.

Troubleshooting

Common Error / IssueCauseFix
InvalidParameterValue error when creating ASGThe <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 launchYour 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 outIt 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.

Ready to study AWS Certified Solutions Architect - Associate (SAA-C03)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free