Practice in your account · 45 min
An Application Load Balancer with Auto Scaling in two zones
In this lab you will put two instances behind an Application Load Balancer, make their Auto Scaling group scale with CPU load and see how it replaces an instance you terminate by hand.
In this lab you will build the web tier of a resilient architecture, which is an architecture that keeps working even if one of its parts fails. It is a topic in domain 2 of the exam, and that tier has two pieces:
- An Application Load Balancer (ALB), which receives all the visits that come from the internet and distributes them across several servers, so that none of them gets overloaded and, if one fails, the visits go to the others.
- An Auto Scaling group, which is a set of instances that AWS keeps at the number you decide, and that adds or removes instances depending on how much work they have.
For example, when an online store gets many more visits during a sale season, its Auto Scaling group adds servers, and the load balancer starts sending them visits as soon as they are ready. When the sale is over, the group removes the extra servers and the store stops paying for them.
In this lab, the ALB is in two public subnets and distributes requests across the group's instances, which are in two private subnets. You will create it with a CloudFormation template, which is a file that describes all the resources so that AWS creates them for you. Then you will send it load so the group adds instances, and terminate an instance by hand to see how the group replaces it.

The load balancer has a node in the public subnet of each Availability Zone and is the only thing that receives traffic from the internet, while the Auto Scaling group spreads its instances across the zones. For example, if one zone fails, the load balancer keeps sending the store's visits to the instances in the other zone. In this lab the architecture uses two zones and only its first two tiers, because the data tier is the Amazon RDS lab.
Before you start
First do the budget lab. This lab charges by the hour while it exists, so do it in a single sitting and delete the stack at the end.
If you have a full account, you can use any Region, which is a place in the world where AWS has its data centers, as long as it has at least two Availability Zones. If your account is a new experience account, your project is in us-east-2, and you don't need a default VPC because the template creates its own VPC.
What the template creates
The [lab07-alb-asg.yaml](/roadmaps/plantillas/lab07-alb-asg.yaml) template creates a VPC, which is a private network of your own inside AWS, with two public subnets and two private ones. In the public subnets it puts an ALB that listens on port 80, the number through which visits to an unencrypted web page arrive. Behind the ALB it puts a target group, which is the group of targets the load balancer sends traffic to and which checks the /salud path every 10 seconds.
That check is called a health check: the target group visits the /salud path of each instance and, if one doesn't respond, stops sending it visits. For example, if the program on one of the store's instances freezes, the load balancer sends the visits only to the instances that do respond, and customers don't see the error.
In the private subnets it puts an Auto Scaling group with a minimum of 2, a desired capacity of 2 and a maximum of 4 t3.micro instances, a small instance type. The minimum is the number of instances the group never goes below, the maximum is the number it never goes above, and the desired capacity is the number the group tries to have at any given moment. For example, with these values the store always has at least two servers serving customers, and at the busiest moment it never pays for more than four.
The group launches each instance with a launch template, which is the recipe for the instance: it says which operating system, which size and which program each new instance has, so that they all come out the same. This launch template requires IMDSv2 and turns on detailed monitoring, which sends each instance's measurements to CloudWatch more often.
On top of the group there is a target tracking policy that keeps the group's average CPU at 50%, so it adds instances when the average goes up and removes them when it goes down. For example, if the store's servers spend several minutes working well above 50%, the group adds an instance to share the work, and when the visits drop, it removes it.
Each instance runs a Python web server, which is a program that answers visits with a page, and that page shows the instance's ID and zone. A path is the part of the web address that comes after the site name, and the /carga path puts the instance's two vCPUs (virtual CPUs) at 100% for 120 seconds. For example, opening the load balancer's address followed by /carga makes whichever instance serves you work at full capacity for that time, as if a lot of customers suddenly arrived.
The instances have no public IP, which is the address something can be reached at from the internet, and no outbound internet access, and they don't need it, because the server doesn't install anything and the only one that talks to them is the load balancer.
Create the stack and check the load balancer
A stack is the set of resources CloudFormation creates from the template. When it finishes, the stack gives you its outputs, which are pieces of data you will need later. For example, the UrlDelBalanceador output is the address the store's customers would open to enter the site.
- In CloudFormation, choose Create stack, With new resources (standard), Upload a template file and upload
lab07-alb-asg.yaml. - Name the stack
cloudpath-saa-lab07-alb-asg. Leave the default parameters and choose Submit. It takes a few minutes. - In the Outputs tab, open
UrlDelBalanceadorand reload the page several times. Each response says which instance served you and in which zone; the zone should alternate between the two. - In the EC2 console, open Target groups, choose the stack's target group and go to the Targets tab. You should see two
healthytargets, one per zone. - Open Auto Scaling groups and choose the stack's group. Check that the capacity is 2, 2 and 4, that the health check type is ELB and, in the automatic scaling tab, that the target tracking policy is there. From there you can reach the two CloudWatch alarms the policy created, one to scale out and one to scale in. Don't edit them.
- Check the security groups. The ALB's group accepts port 80 from the internet; the instances' group only accepts port 80 when it comes from the ALB's group.
A healthy target is an instance that passed the /salud health check. The group's health check type being ELB means the group uses that same load balancer check to decide whether an instance is working, and replaces any that doesn't respond. For example, if the server on one of the store's instances stops answering on /salud, the load balancer stops sending it visits and the group swaps it for a new one.
Security groups are each server's rules that say which traffic can come in. For example, someone on the internet can open the store's load balancer address, but can't reach an instance directly, because the instances' security group only accepts what comes from the load balancer.
Trigger scaling
![]()
A target tracking policy doesn't react to every spike. CloudWatch averages the group's CPU, the alarm goes off when that average goes above the target, and then the group changes its desired capacity. A CloudWatch alarm is a rule that watches a measurement and changes to the ALARM state when the measurement goes past a limit. For example, if one store customer causes a moment of high CPU, the group does nothing, but if the store spends several minutes under heavy load, the average goes up, the alarm goes off and the group adds instances.
What does the group do?
For several minutes, the group's average CPU stays well above 50%. What happens?
To trigger scaling, first save the stack's data in variables and then send load to the instances' /carga path. A variable is a name that holds a value in CloudShell, so you don't have to copy that value into every command. For example, after step 7, typing $URL in a command is the same as typing the load balancer's full address.
- Open AWS CloudShell and save the load balancer URL and the group name in two variables:
bash
URL=$(aws cloudformation describe-stacks --stack-name cloudpath-saa-lab07-alb-asg --query "Stacks[0].Outputs[?OutputKey=='UrlDelBalanceador'].OutputValue" --output text)
GRUPO=$(aws cloudformation describe-stacks --stack-name cloudpath-saa-lab07-alb-asg --query "Stacks[0].Outputs[?OutputKey=='NombreDelGrupo'].OutputValue" --output text)
- Generate load with this command. Repeat it every minute for about five minutes:
bash
for i in $(seq 1 10); do curl -s "$URL/carga"; echo; done
- Watch the group's activity:
bash
aws autoscaling describe-scaling-activities --auto-scaling-group-name "$GRUPO" --max-items 5 \
--query "Activities[].[StartTime,StatusCode,Description]" --output table
When the scale-out alarm goes to ALARM, the group will raise its desired capacity and the new instances will show up in the target group. When you stop sending load, the group will go back to 2, and it scales in more slowly than it scaled out.
-
Terminate an instance by hand and watch how the group replaces it:
bash ID=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names "$GRUPO" --query "AutoScalingGroups[0].Instances[0].InstanceId" --output text) aws ec2 terminate-instances --instance-ids "$ID"In the group's Activity tab you will see the termination and, shortly after, the launch of a new instance to get back to the desired capacity.
The load in this lab costs almost nothing, although t3 instances start in unlimited mode and charge for surplus credits if their 24-hour average CPU goes above the baseline. The baseline is the share of the CPU a t3 instance can use all the time without paying extra, and in unlimited mode the instance can go above it when it needs to, although AWS charges the difference if the day's average ends up above it. For example, a few minutes at 100% cost a few cents at most, because the rest of the day the instance barely works and the 24-hour average stays low.
True or false?
0 of 3If you terminate an instance by hand, the group launches another one to get back to the desired capacity.
When you stop sending load, the group can end up with a single instance.
The group's instances receive requests directly from the internet.
How to know you're done
When you finish, describe-scaling-activities has to show at least two launches: one caused by the alarm and another one caused by the instance you terminated. The target group has two healthy targets again, one in each zone.
Delete what you created
To delete the lab, in CloudFormation select cloudpath-saa-lab07-alb-asg and choose Delete. That terminates the group's instances and deletes the ALB, the target group and the VPC. Then check in EC2 that no cloudpath-saa-lab07-web instance is left running and that the stack's load balancer no longer appears under Load balancers.
Next
- Tutorial: Set up a scaled and load-balanced application: https://docs.aws.amazon.com/autoscaling/ec2/userguide/tutorial-ec2-auto-scaling-load-balancer.html
- cloud path practice exams: /examenes?lang=en