AWS Builder Center

What I Learned Building a Secure Three-Tier Web Architecture on AWS

A student project exploring how public and private subnets, an Application Load Balancer, Amazon EC2, Amazon RDS, security groups, AWS WAF and CloudFormation work together to create a more secure web architecture.

Introduction

As a cybersecurity student, I wanted to understand AWS security beyond simply launching an EC2 instance and opening a web page. For one of my cloud projects, I built a three-tier web architecture using an Application Load Balancer, a PHP application hosted on Amazon EC2 and a MySQL database on Amazon RDS.
The main objective was to make the application reachable from the internet without directly exposing the application server or database. I also wanted the infrastructure to be repeatable, so I defined the resources using AWS CloudFormation instead of creating everything manually.
This article shares the architecture I built, the security decisions behind it, the problems I encountered and the most important lessons I learned.

The Architecture

The application followed three logical tiers:
  1. Presentation tier: An internet-facing Application Load Balancer received requests from users.
  1. Application tier: An EC2 instance in a private subnet hosted the PHP web application.
  1. Database tier: An Amazon RDS for MySQL instance stored the application’s data in private database subnets.
The request flow was:
User → AWS WAF → Application Load Balancer → EC2 application → RDS MySQL
The VPC contained public and private subnets. The load balancer and NAT gateway were placed on the public side of the network, while the EC2 application server and RDS database remained private.
AWS defines a private subnet as one without a direct route to an internet gateway. A NAT gateway can still provide outbound connectivity for private instances without allowing external systems to initiate connections to them. You can learn more from the  AWS VPC documentation .
For a student project, I used a limited deployment to control costs. In a production environment, I would deploy application instances across at least two Availability Zones with Auto Scaling and use a highly available database configuration.

Why I Used an Application Load Balancer

Instead of assigning a public IP address to the EC2 instance, users accessed the application through an Application Load Balancer. The load balancer became the public entry point and forwarded requests to the EC2 target group.
This design provided two important benefits. First, the EC2 instance did not need to be directly reachable from the internet. Second, the load balancer could perform health checks and forward traffic only when the application target was healthy.
AWS explains that an Application Load Balancer distributes incoming traffic across registered targets and monitors their health. More information is available in the  Elastic Load Balancing documentation .
I also associated an AWS WAF web ACL with the load balancer. This added another inspection layer for HTTP requests before they reached the application. AWS WAF works by evaluating web requests against the rules in a web ACL associated with a protected resource. This process is explained in the  AWS WAF documentation .

Applying Least-Privilege Network Access

One of the most useful lessons from this project was that subnet placement alone is not enough. Security groups must also restrict which tier is allowed to communicate with the next tier.
I used separate security groups with a flow similar to this:
Security group
Permitted inbound traffic
Load balancer security group
HTTP or HTTPS from users
EC2 security group
Application traffic only from the load balancer security group
RDS security group
MySQL on port 3306 only from the EC2 security group
This created a controlled chain of communication. The database did not accept connections from the public internet or arbitrary resources inside the VPC. Only the application tier could initiate a database connection.
Amazon RDS supports using another VPC security group as the source of a database rule, allowing access from application servers that use that security group. More information is available in the  Amazon RDS documentation .
I also attached an IAM role to the EC2 instance instead of placing long-term AWS access keys inside the application. This reinforced an important cloud-security principle: permissions should be assigned to the workload through a role and limited to only the actions it requires.

Using CloudFormation Instead of Manual Deployment

I defined the infrastructure using AWS CloudFormation templates. CloudFormation allows AWS resources to be modelled and provisioned as infrastructure as code, as explained in the  AWS CloudFormation documentation .
Rather than keeping every resource in one large template, I separated the deployment into stacks based on responsibility. The networking resources could be created first, followed by the security, database and application components. Stack outputs made important identifiers available to the stacks that depended on them.
This approach taught me why deployment order and dependencies matter. For example, the application could not be configured correctly until the network and security groups existed. The load balancer also required a target group and a healthy application endpoint.
CloudFormation made troubleshooting easier because stack events showed which resource had failed. More importantly, I could recreate the architecture consistently instead of relying on memory and manual console steps.
AWS recommends treating CloudFormation templates like application code by storing them in version control, reviewing changes and validating updates. These recommendations are available in the  CloudFormation best practices .

Testing the Architecture

After deployment, I performed several checks instead of assuming that successful stack creation meant the system was secure.
  • I accessed the application using the load balancer’s DNS name.
  • I checked the target group’s health status.
  • I confirmed that the EC2 instance was not directly accessible from the internet.
  • I verified that the RDS instance was not publicly accessible.
  • I tested that only the EC2 security group could connect to the MySQL port.
  • I reviewed AWS WAF behaviour using controlled web requests.
  • I checked CloudTrail and CloudWatch information to confirm that important activities were visible.
These tests showed me the difference between deployment and validation. A resource can be running while still being incorrectly exposed, unreachable or poorly monitored.

Challenges and Lessons Learned

The most challenging part was understanding how routes, subnets, security groups, target groups and health checks interact. A single incorrect rule could cause the load balancer to report an unhealthy target even when the application was running.
I also learned to consider cost as part of architecture design. Services such as NAT Gateway and AWS WAF may continue generating charges while provisioned. For temporary learning environments, resources should be monitored and removed after testing.
CloudFormation helped because deleting the stacks removed most related resources in a controlled way. However, retained or dependent resources still needed to be checked manually.
If I continued developing this architecture, my next improvements would include:
  • Running EC2 instances across multiple Availability Zones using Auto Scaling.
  • Configuring HTTPS with an AWS Certificate Manager certificate.
  • Storing database credentials in AWS Secrets Manager.
  • Enabling stronger database backup and Multi-AZ options.
  • Adding more detailed CloudWatch alarms and centralised logging.
  • Integrating automated template validation into a CI/CD pipeline.

Conclusion

This project helped me understand that cloud security is not provided by one service. It comes from combining network isolation, controlled traffic paths, least-privilege permissions, web-request filtering, monitoring and repeatable deployment.
My biggest takeaway was simple: only make the component that must be public accessible from the internet. In this architecture, that component was the Application Load Balancer. The application and database remained behind controlled security boundaries.
For students learning AWS, I recommend building a small architecture like this and testing every assumption. Do not only ask, “Does the application work?” Also ask, “Which components are reachable, who can access them and what evidence proves that the controls are working?”
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article