Build an Elastic Web Tier
Build and test a load-balanced web tier that scales with traffic.
Introduction
30 Second Summary
A website can feel fast when one person visits it. A sudden crowd can overwhelm the same server in minutes.
In this project, you will build an Amazon EC2 Auto Scaling web tier through the AWS console. You will route requests through an Application Load Balancer, generate traffic, and watch healthy capacity grow from one server to two.
What You'll Build
You will open one public URL, generate a burst of requests, and watch a second healthy backend join the pool without taking the page offline.
By the end of this project, you'll have:
- A public web page that stays reachable through an Application Load Balancer while EC2 Auto Scaling manages its backends.
- A repeatable traffic test that pushes request demand above the scaling target and adds a second instance.
- Visible health-check evidence showing both backends are ready before the load balancer sends them requests.
- Secret Mission: Rebuild the same load-balanced Auto Scaling web tier with AWS CLI commands in CloudShell.
What do I need before I start?
You need an AWS account with permission to manage EC2, Elastic Load Balancing, Auto Scaling, security groups, and CloudShell.
You do not need CloudFormation experience. The main project builds each component through the AWS console before the optional CLI rebuild.
These resources can generate usage-based charges. The cleanup section removes every project resource when you finish.
Before We Start
A clear goal connects every resource you create to the proof you want to see. Before the hands-on work begins, this is your moment to commit to the load-balanced web tier you are about to build.
Prepare the AWS Environment
Your load balancer needs two public subnets in different Availability Zones. Your instances also need firewall rules that accept web traffic only from that load balancer.
This step prepares that network boundary and opens AWS CloudShell. You finish with the foundation needed to launch the web tier safely.
In this step, get ready to:
- Confirm the default VPC has public subnets in at least two Availability Zones.
- Create separate security groups for the load balancer and web instances.
- Verify that CloudShell can run the traffic tool.
Confirm the default network
Amazon VPC provides the network where your load balancer and instances run. A default VPC includes a public subnet in each Availability Zone, which keeps this short lab focused on scaling.
- Sign in to the AWS Management Console.
- Choose US East (N. Virginia) from the region selector in the top navigation.
- Enter VPC in the console search bar.
- Select VPC from the search results.
- Choose Your VPCs in the left navigation.
- Check whether one row shows Yes in the Default VPC column.
Choose the tab that matches what you see.
✔️ Default VPC exists
- Choose Subnets in the left navigation.
- Find two rows that show Yes in the Default subnet column.
- Confirm the two rows use different values in the Availability Zone column.
Your default network can support an Application Load Balancer across two locations.
ⓧ Default VPC is missing
Restoring a default VPC looks significant, but it only recreates the standard network that AWS normally supplies for this Region.
- Choose Actions on the Your VPCs page.
- Choose Create default VPC.
- Choose Create.
- Choose Subnets after creation finishes.
- Confirm two default subnets use different Availability Zones.
Create the traffic security groups
A security group controls which traffic can reach a resource. The load balancer accepts public HTTP requests, while the instances accept HTTP only from the load balancer.
- Enter EC2 in the console search bar.
- Select EC2 from the search results.
- Choose Security Groups under Network & Security.
- Choose Create security group.
- Enter elastic-web-alb-sg for the security group name.
- Select the default VPC.
- Add an inbound HTTP rule with source Anywhere-IPv4.
- Choose Create security group.
The first group is the public entrance. The second group keeps direct internet traffic away from the web instances.
- Return to the Security Groups page.
- Choose Create security group.
- Enter elastic-web-instance-sg for the security group name.
- Select the default VPC.
- Add an inbound HTTP rule.
- Choose Custom as the source.
- Select elastic-web-alb-sg as the source security group.
- Choose Create security group.
Check CloudShell
CloudShell provides a browser-based Bash terminal with your AWS credentials already available. You use its curl command to generate traffic later.
- Choose the CloudShell icon in the console toolbar.
- Wait for the shell prompt to become available.
- Check that curl is available by running:
curl --version
You should see curl version information. Your browser-based traffic generator is ready.
No version information?
Refresh the CloudShell session if the terminal never reaches a prompt. Confirm the console region is still US East (N. Virginia).
Help me troubleshoot the CloudShell check.
Your network and traffic boundaries are ready. Next, you will create the launch template and watch Auto Scaling start the first web server.
Deploy the Web Tier
Launch the First Web Server
Your network now separates public load-balancer traffic from private backend access. The next piece is a repeatable server definition.
An EC2 launch template stores the image, instance size, security group, and startup script. An Auto Scaling group uses that template whenever it needs another server.
In this step, get ready to:
- Create a launch template that installs and starts the web server.
- Create an Auto Scaling group with room to grow from one instance to two.
- Verify the first instance reaches the active lifecycle state.
Create the launch template
The startup script installs Apache each time Auto Scaling launches an instance. It also writes a health endpoint and includes the backend hostname on the main page.
- Choose Launch Templates under Instances in the EC2 navigation.
- Choose Create launch template.
- Enter elastic-web-template for the launch template name.
- Select the latest Amazon Linux 2023 AMI from Quick Start.
- Select t3.micro as the instance type.
- Select Don't include in launch template for the key pair.
- Select elastic-web-instance-sg under Firewall (security groups).
- Expand Advanced details.
- Configure every launched server by pasting this into User data:
#!/bin/bash
# Install and start Apache.
sudo dnf install -y httpd wget php-fpm php-mysqli php-json php php-devel
sudo systemctl enable httpd
sudo systemctl start httpd
# Make the backend visible in responses.
HOST_NAME=$(hostname)
cat > /var/www/html/index.html <<EOF
<!doctype html>
<html><body><h1>Elastic Web Tier</h1><p>Served by $HOST_NAME</p></body></html>
EOF
# Give the load balancer a dedicated health endpoint.
echo "healthy" > /var/www/html/health.html
What does the startup script do?
- httpd serves the web page over HTTP.
- HOST_NAME makes each backend distinguishable when requests are distributed.
- /health.html gives the load balancer a small file to check without depending on page content.
- Choose Create launch template.
- Confirm the success page names elastic-web-template.
Launch template not created?
Check that the security group is selected by ID rather than typed as a name. Confirm that the first line of the user data starts with #!/bin/bash.
Help me troubleshoot the launch template.
Create the Auto Scaling group
The group begins with one server and can grow to two. It spans the same two Availability Zones that the load balancer uses later.
- Choose Auto Scaling Groups under Auto Scaling in the EC2 navigation.
- Choose Create Auto Scaling group.
- Enter elastic-web-asg for the group name.
- Select elastic-web-template as the launch template.
- Choose Next.
- Select the default VPC.
- Select two default subnets in different Availability Zones.
Why leave load balancing empty?
The group is created without a load balancer for the moment. The next step builds the target group and connects the traffic path.
- Choose Next until you reach the group size settings.
- Enter 1 for Desired capacity.
- Enter 1 for Min desired capacity.
- Enter 2 for Max desired capacity.
- Continue without adding a scaling policy.
- Choose Create Auto Scaling group on the review page.
Verify the first instance
Instance startup usually takes a few minutes while Amazon Linux installs Apache. A short period in Pending is expected.
Before you refresh, which lifecycle state do you expect after the launch finishes?
- Select elastic-web-asg.
- Choose the Instance management tab.
- Refresh until one instance shows InService.
- Choose the Activity tab.
- Confirm the latest launch activity shows Successful.
That is your first scaling building block working. The group can now launch a configured web server from one reusable template.
Instance not reaching InService?
Allow several minutes for package installation. If the launch fails, open the Activity tab and read the cause beside the newest entry.
Help me diagnose the instance launch.
Your first backend is active. Next, you will put an Application Load Balancer in front of it and use health checks to decide when it can receive traffic.
Identify the Scaling Gap
Add Load Balancing and Health Checks
Your Auto Scaling group can launch a web server, but visitors still have no shared entrance. The group also relies only on EC2 health signals.
This step adds an Application Load Balancer and a target group with HTTP health checks. Auto Scaling then uses load-balancer health to replace a backend that stops serving the application.
In this step, get ready to:
- Create a target group with an explicit health-check path.
- Create an internet-facing Application Load Balancer.
- Attach the target group and verify the first healthy response.
Create the target group
A target group is the pool of backends behind a load balancer. Its health check decides which instances are ready to receive requests.
- Choose Target Groups under Load Balancing in the EC2 navigation.
- Choose Create target group.
- Select Instances as the target type.
- Enter elastic-web-targets for the target group name.
- Select HTTP on port 80.
- Select the default VPC.
- Enter /health.html for the health-check path.
- Expand Advanced health check settings.
How will health checks work?
The dedicated path returns a small 200 response after Apache finishes starting. Two successful checks admit a backend quickly without routing traffic to an unready server.
- Enter 2 for the healthy threshold.
- Enter 2 for the unhealthy threshold.
- Enter 5 seconds for the timeout.
- Enter 30 seconds for the interval.
- Enter 200 for the success code.
- Choose Next.
- Skip manual target registration.
- Choose Create target group.
Create the load balancer
The load balancer provides one public address across two Availability Zones. Its listener forwards each HTTP request to a healthy target.
- Choose Load Balancers under Load Balancing.
- Choose Create load balancer.
- Choose Create under Application Load Balancer.
- Enter elastic-web-alb for the load balancer name.
- Keep the Internet-facing scheme.
- Select the default VPC.
- Select two default subnets in different Availability Zones.
- Select elastic-web-alb-sg as the security group.
- Keep the HTTP listener on port 80.
- Select elastic-web-targets as the default forwarding target.
- Choose Create load balancer.
Expect the load balancer to remain in Provisioning for a few minutes. AWS is creating endpoints in both selected Availability Zones.
Attach the group and enable health checks
Attaching the target group makes Auto Scaling register every current and future instance automatically. Enabling Elastic Load Balancing health checks lets the group replace an instance that fails the application check.
- Open Auto Scaling Groups in the EC2 navigation.
- Select elastic-web-asg.
- Choose the Integrations tab.
- Choose Edit under Load balancing.
- Select elastic-web-targets as the Application Load Balancer target group.
- Choose Update.
- Choose the Details tab.
- Choose Edit under Health checks.
- Turn on Elastic Load Balancing health checks.
- Enter 300 seconds for the health check grace period.
- Choose Update.
The first health result can take two or three minutes. The target stays in initial until it passes the configured checks.
Before you refresh, do you expect the target to become healthy before or after Apache answers /health.html?
- Open Target Groups.
- Select elastic-web-targets.
- Choose the Targets tab.
- Refresh until one registered target shows healthy.
- Open Load Balancers.
- Select elastic-web-alb.
- Copy the DNS name and add http:// at the start.
- Record the address here: http://your-load-balancer-dns-name.
- Open the recorded address in a new browser tab.
You should see Elastic Web Tier and a backend hostname. That response proves the listener reached a target that passed the health check.
Target not becoming healthy?
Confirm the target group's health-check path is /health.html. Check that the instance security group accepts HTTP from elastic-web-alb-sg.
Help me diagnose the unhealthy target.
Your application now has one public URL and one healthy backend. Next, you will test whether light traffic is enough to make the group grow.
Generate Traffic and Scale Out
Your load balancer now serves the web page through one healthy target. The Auto Scaling group still needs a rule that turns request demand into capacity.
You will create a target-tracking policy based on Application Load Balancer requests per target. You will first prove that light traffic leaves the group unchanged, then sustain enough traffic to trigger scale-out.
In this step, get ready to:
- Create a request-count target-tracking policy.
- Observe that a few browser requests do not trigger scaling.
- Generate sustained traffic until a second instance reaches InService.
Create the scaling policy
The ALBRequestCountPerTarget metric measures average requests handled by each target during a one-minute interval. The value 20 is intentionally low for this short learning test.
- Open Auto Scaling Groups in the EC2 console.
- Select elastic-web-asg.
- Choose the Automatic scaling tab.
- Choose Create dynamic scaling policy.
- Select Target tracking scaling as the policy type.
- Enter elastic-web-request-tracking for the policy name.
- Select Application Load Balancer request count per target as the metric.
- Select the resource label for elastic-web-alb and elastic-web-targets.
- Enter 20 as the target value.
- Choose Create.
Why use request count?
Target tracking aims to keep average throughput near the target value. AWS manages the CloudWatch alarms that add or remove instances for this policy.
Expose the light-traffic gap
Five browser refreshes are below the configured target during a one-minute interval. This gives you a baseline before the sustained test.
Before you refresh, do you think five requests will change the desired capacity?
- Open http://your-load-balancer-dns-name in your browser.
- Refresh the page five times.
- Return to elastic-web-asg.
- Choose the Instance management tab.
- Refresh the group view.
You should still see one InService instance. The light traffic stayed below the request target.
Generate sustained traffic
A short burst can disappear inside one metric interval. Three request batches keep demand elevated long enough for the policy to react.
This test creates billable capacity
This test can launch a second t3.micro instance. The load balancer, public IPv4 addresses, and compute can generate usage-based charges until cleanup.
The traffic loop takes a little over three minutes. CloudShell stays mostly quiet while the requests run, so silence does not mean it has stopped.
Before you run the loop, do you expect sustained requests to produce a different result from five browser refreshes?
- Open CloudShell from the console toolbar.
- Generate three request batches by running:
export LAB_URL='[[APPLICATION_URL="http://your-load-balancer-dns-name"]]'
for minute in 1 2 3; do
for request in {1..100}; do
curl --silent --output /dev/null "$LAB_URL"
done
sleep 60
done
How does the traffic loop work?
- LAB_URL stores your load balancer address for the loop.
- The inner loop sends 100 requests during each round.
- sleep 60 spreads the traffic across three one-minute metric intervals.
- --output /dev/null discards each response body while the load balancer still counts the request.
- Return to the Activity tab for elastic-web-asg after the CloudShell prompt returns.
- Refresh until the newest launch activity shows Successful.
- Choose the Instance management tab.
- Refresh until two instances show InService.
Scaling can take several minutes after the loop finishes. The policy needs metric data, then EC2 needs time to launch the second server.
That is the scale-out working. Sustained requests raised the group from one active backend to two.
No new launch activity?
Confirm the traffic loop used the complete load balancer URL. Wait through another one-minute metric interval before checking Activity again.
Help me diagnose missing scale-out.
Your group now has two active servers. Next, you will prove that both pass the application health check and share traffic behind one URL.
Verify Healthy Load Balancing
Sustained traffic has expanded your Auto Scaling group to two InService instances. Lifecycle state proves capacity exists, but it does not prove the application can answer requests.
You now verify both target-group health checks and the live response path. Together, those signals show that the Application Load Balancer can use the expanded backend pool.
In this step, get ready to:
- Confirm both registered targets pass the HTTP health check.
- Send repeated requests through the load balancer.
- Compare backend hostnames while the application remains available.
Confirm both targets are healthy
A target becomes eligible for traffic only after /health.html returns the configured success code. The second instance may need another minute to finish its first checks.
- Open Target Groups in the EC2 console.
- Select elastic-web-targets.
- Choose the Targets tab.
- Refresh until two registered targets show healthy.
- Confirm both targets use port 80.
Both backends have now passed the application check. The load balancer can include either instance when it selects a target.
What does healthy prove?
An InService instance has joined the Auto Scaling group. A healthy target has also answered the load balancer's HTTP checks.
The second signal proves that the web process and network path are working.
Observe the load-balanced responses
Each page includes the hostname of the backend that served it. Repeated requests let you observe the load balancer selecting from the healthy pool.
Before you run the check, do you expect every response to show the same backend hostname?
- Open CloudShell from the console toolbar.
- Store the load balancer URL and send twelve requests by running:
export LAB_URL='[[APPLICATION_URL="http://your-load-balancer-dns-name"]]'
for request in {1..12}; do
curl --silent "$LAB_URL"
echo
done
What should you see?
Every response should contain Elastic Web Tier. Across the responses, you should see the hostnames of both healthy backends.
A hostname can repeat because each request is an independent routing decision. Run the loop again if the first sample reaches only one target.
- Open http://your-load-balancer-dns-name in your browser.
- Reload the page several times.
The page should remain available while requests move through the two-target pool. You have now verified scaling, health checks, and load balancing as one working path.
Requests not reaching both backends?
Confirm both targets still show healthy. Check that LAB_URL begins with http:// and contains the current load balancer DNS name.
Help me troubleshoot the load-balanced responses.
Secret mission
Rebuild the Web Tier with AWS CLI
Rebuild the load-balanced Auto Scaling web tier with AWS CLI commands. Configure ELB health checks and prove that sustained requests create two healthy targets.
Clean Up Your Resources
Clean Up Your Resources
Live AWS resources can keep generating usage-based charges after the lab ends. Choose whether to keep your environment running, pause its compute capacity, or delete it entirely.
Cost warning
- The Application Load Balancer can continue generating hourly charges.
- Each running Amazon EC2 instance can continue generating compute charges.
- Attached Amazon EBS volumes can continue generating storage charges.
- Public IPv4 addresses can continue generating address charges.
- Load balancer capacity unit usage can add charges.
- Data transfer can add charges.
Pausing stops the EC2 compute capacity after the instances terminate. The load balancer can still generate charges.
Deleting the stack removes its live resources. Choose this option when you have finished the lab.
Resources you used:
- The elastic-web-asg Auto Scaling group and its managed EC2 instances.
- The elastic-web-template launch template.
- The elastic-web-alb Application Load Balancer and HTTP listener.
- The elastic-web-targets target group and its health-check configuration.
- The elastic-web-request-tracking policy and its managed CloudWatch alarms.
- The elastic-web-alb-sg and elastic-web-instance-sg security groups.
- The optional cli-elastic-web resources created during the Secret Mission.
- The optional cli-user-data.sh and cli-launch-template.json files in CloudShell.
Keep everything running
No action is needed. Choose this if you are still testing the web tier and accept that usage-based charges continue.
- Keep elastic-web-asg and its target-tracking policy active.
- Keep the load balancer and target group available at the recorded application URL.
- Keep any optional cli-elastic-web resources only while you are actively using them.
Pause - I'll come back to this later
Reduce the Auto Scaling groups to zero instances if you plan to return soon. The load balancers and their public addresses can still generate charges while paused.
- Open Auto Scaling Groups in the EC2 console.
- Select elastic-web-asg.
- Choose Edit under the group size settings.
- Set minimum capacity to 0.
- Set desired capacity to 0.
- Choose Update.
- Wait until the Instance management tab has no instances.
If you completed the Secret Mission, pause its group by running:
aws autoscaling update-auto-scaling-group \
--auto-scaling-group-name cli-elastic-web-asg \
--min-size 0 \
--desired-capacity 0
- Verify that both groups show desired capacity 0 before leaving the project.
Delete - I don't want to use this again
Deleting these resources is permanent for both project environments. Your AWS account and default VPC remain available.
Delete the Console-Built Resources
- Open Auto Scaling Groups in the EC2 console.
- Select elastic-web-asg.
- Choose Delete.
- Enter delete when the confirmation field requests it.
- Confirm the deletion.
- Wait until the group and its managed instances disappear.
Deleting the group also removes its scaling policy and managed alarm actions. The load balancer and target group remain until you remove them separately.
- Open Load Balancers.
- Select elastic-web-alb.
- Choose Actions.
- Choose Delete load balancer.
- Confirm the deletion.
- Wait until elastic-web-alb no longer appears.
- Open Target Groups.
- Select elastic-web-targets.
- Choose Actions.
- Choose Delete.
- Confirm the deletion.
- Open Launch Templates.
- Select elastic-web-template.
- Choose Actions.
- Choose Delete template.
- Confirm the deletion.
Delete the Optional CLI Resources
Skip this part if you did not complete the Secret Mission. Otherwise, open CloudShell and remove the group plus load balancer by running:
CLI_LB_ARN=$(aws elbv2 describe-load-balancers \
--names cli-elastic-web-alb \
--query 'LoadBalancers[0].LoadBalancerArn' --output text)
CLI_TG_ARN=$(aws elbv2 describe-target-groups \
--names cli-elastic-web-targets \
--query 'TargetGroups[0].TargetGroupArn' --output text)
aws autoscaling delete-auto-scaling-group \
--auto-scaling-group-name cli-elastic-web-asg \
--force-delete
aws elbv2 delete-load-balancer \
--load-balancer-arn "$CLI_LB_ARN"
The force deletion terminates the CLI-managed instances. Wait until cli-elastic-web-asg and cli-elastic-web-alb no longer appear in the console.
- Remove the remaining CLI resources and CloudShell files by running:
aws elbv2 delete-target-group \
--target-group-arn "$CLI_TG_ARN"
aws ec2 delete-launch-template \
--launch-template-name cli-elastic-web-template
rm -f cli-user-data.sh cli-launch-template.json
Delete the Shared Security Groups
The security groups are shared by the console and CLI builds, so remove them only after both environments are gone.
- Open Security Groups in the EC2 console.
- Select elastic-web-instance-sg.
- Choose Actions.
- Choose Delete security groups.
- Confirm the deletion.
- Repeat the deletion for elastic-web-alb-sg.
Your project load balancers, instances, target groups, templates, policies, and security groups are now removed.
Nice Work!
Nice Work!
Excellent work! You built and tested a load-balanced Amazon EC2 Auto Scaling web tier that adds healthy capacity when request demand rises.
You've learned how to:
- Build a repeatable web server template that installs Apache and exposes a dedicated health-check endpoint.
- Route one public URL through an Application Load Balancer to backends that pass HTTP health checks.
- Prove traffic-driven scale-out by pushing ALBRequestCountPerTarget above its target until two instances reached InService and healthy.
- Secret Mission: Rebuild the launch template, target group, load balancer, listener, Auto Scaling group, health checks, and scaling policy with AWS CLI commands.
Ready to quiz yourself?