
Deploying a Web Application Using EC2 and Application Load Balancer on AWS
Learn how to deploy a web application on AWS using Amazon EC2 and an Application Load Balancer. Explore VPC setup, public and private subnets, target groups, health checks, security groups, and Auto Scaling in this beginner-friendly guide.
Series: AWS in the Real World: Building, Securing & Optimizing the Cloud (6 articles)
- …
- 6Deploying a Web Application Using EC2 and Application Load Balancer on AWS This article
Deploying a Web Application Using EC2 and Application Load Balancer on AWS
Introduction
Imagine you've developed a web application and now want to make it accessible to users over the internet.
You could launch a single EC2 instance, install your application, and allow users to access it directly.
That might work for a small experiment.
But what happens when more users visit your application? What if your EC2 instance becomes unhealthy? How can you distribute incoming traffic across multiple servers?
This is where Amazon EC2 and Application Load Balancer (ALB) become useful.
Amazon EC2 provides the computing capacity to run your application, while an Application Load Balancer distributes incoming requests across available application servers.
In this article, we'll explore how these services work together and understand the basic steps involved in deploying a web application on AWS.
1. What Is Amazon EC2?
Amazon Elastic Compute Cloud (EC2) is an AWS service that allows you to launch virtual servers in the cloud.
These virtual servers are called EC2 instances.
You can use EC2 instances to run:
- Websites and web applications.
- Backend services and APIs.
- Development and testing environments.
- Application servers.
- Business software.
When launching an EC2 instance, you can choose:
- Operating system, such as Amazon Linux or Ubuntu.
- Instance type, which determines computing resources.
- Storage capacity.
- Network configuration.
- Security settings.
For example, if you're hosting a simple web application, you might start with a small EC2 instance and increase its capacity as your application grows.
EC2 gives you control over the server, but you're also responsible for managing its operating system, updates, application software, and configuration.
2. What Is an Application Load Balancer?
An Application Load Balancer is an AWS service that distributes incoming application traffic across multiple targets, such as EC2 instances.
Think of it as a traffic manager.
Imagine your application is running on three EC2 instances.
Instead of sending every user directly to one server, the load balancer receives incoming requests and forwards them to healthy application instances.
This provides several benefits:
- Distributes incoming traffic.
- Helps avoid sending requests to unhealthy targets.
- Supports scaling by adding more application instances.
- Provides a single entry point for users.
- Supports advanced routing based on request information.
An Application Load Balancer operates at the application layer (Layer 7) and supports HTTP and HTTPS traffic.
3. Why Use EC2 with an Application Load Balancer?
Let's consider two different deployment approaches.
Without a Load Balancer
INTERNET | | EC2 Instance (Application)In this setup, users access a single EC2 instance directly.
If the instance becomes unavailable, users may not be able to access the application.
You also have to manage traffic and availability yourself.
With an Application Load Balancer
INTERNET | | APPLICATION LOAD BALANCER | +-----------+-----------+ | | | | | | EC2-1 EC2-2 EC2-3 Healthy Healthy HealthyNow the load balancer distributes requests across multiple EC2 instances.
If one instance becomes unhealthy, the load balancer can stop sending new requests to that target while healthy targets continue serving traffic.
This makes the architecture more resilient.
Important: A load balancer alone does not guarantee high availability. You should deploy targets across multiple Availability Zones and consider using Auto Scaling to replace unhealthy instances and adjust capacity.
4. Understanding the AWS Architecture
Before deploying the application, let's understand the components involved.
A typical setup might include:
- Amazon VPC: Provides the network environment.
- Public subnets: Host the internet-facing Application Load Balancer.
- Private subnets: Host EC2 application instances.
- Application Load Balancer: Receives and distributes incoming requests.
- Target group: Identifies the EC2 instances that receive traffic.
- Security groups: Control which resources are allowed to communicate.
- Amazon EC2: Runs the web application.
A simplified architecture looks like this:
AWS Architecture Diagram
INTERNET USERS | v APPLICATION LOAD BALANCER | ------------------- | | v v AVAILABILITY ZONE 1 AVAILABILITY ZONE 2 | | +--------------+ +--------------+ | Public | | Public | | Subnet | | Subnet | +--------------+ +--------------+ | | v v +--------------+ +--------------+ | Private | | Private | | Subnet | | Subnet | | | | | | EC2 Instance | | EC2 Instance | | 1 | | 2 | +--------------+ +--------------+ ^ ^ | | +--------+--------+ | TARGET GROUPThe Application Load Balancer is configured across public subnets, while its EC2 targets can run in private subnets.
For better availability, the load balancer should use subnets in at least two Availability Zones, and the application instances should also be distributed across multiple Availability Zones.
5. Step-by-Step: Deploying a Web Application on AWS
Let's walk through the basic deployment process.
You don't need to create a complicated production environment to understand the workflow. Start with a simple application and gradually add more components.
Step 1: Create an Amazon VPC
First, create a VPC to provide the network environment for your application.
You will need:
- A VPC with an appropriate IP address range.
- Public subnets for the Application Load Balancer.
- Private subnets for EC2 instances.
- Route tables to control network traffic.
- An Internet Gateway for internet connectivity.
For a more reliable design, create the public and private subnets across at least two Availability Zones.
Step 2: Launch EC2 Instances
Next, launch EC2 instances to run your web application.
During instance creation, configure:
- An appropriate operating system.
- An instance type.
- Storage.
- VPC and private subnet placement.
- An IAM role if the application needs to access other AWS services.
- A security group that allows application traffic from the load balancer.
You can install your web server and application software on these instances.
For example, you could use a simple web application running behind a web server.
Beginner tip: You don't need to expose your EC2 instances directly to the internet. In this architecture, users access the application through the load balancer.
Step 3: Create a Target Group
A target group tells the Application Load Balancer where to send incoming requests.
For this example, your targets will be EC2 instances.
When creating a target group:
- Choose the target type.
- Select the protocol and port used by your application.
- Configure a health check path, such as
/.
- Register your EC2 instances.
The load balancer uses health checks to determine whether registered targets are healthy enough to receive traffic.
For example, if your application responds successfully to requests at the configured health check path, the instance can be marked healthy.
Step 4: Create an Application Load Balancer
Now create an Application Load Balancer.
During configuration:
- Choose the internet-facing scheme.
- Select your VPC.
- Choose public subnets in at least two Availability Zones.
- Attach an appropriate security group.
- Create a listener for HTTP or HTTPS.
- Configure the listener to forward requests to your target group.
The load balancer will receive incoming requests and forward them to healthy targets.
Step 5: Configure Security Groups
Security groups are important because they control network access to your resources.
A recommended setup is:
Load Balancer Security Group
- Allow HTTP traffic on port 80 from the internet if your application supports HTTP.
- Allow HTTPS traffic on port 443 from the internet when HTTPS is configured.
- Allow only the traffic your application needs.
EC2 Security Group
- Allow application traffic only from the load balancer's security group.
- Avoid allowing direct public access to application ports.
- Allow administrative access through a secure management method when required.
For example, if your application listens on port 80, the EC2 security group should allow port 80 from the load balancer's security group rather than from all internet addresses.
This creates a more secure communication path.
Step 6: Test Your Application
Once the load balancer is active and your EC2 instances are registered:
- Open the EC2 console.
- Find your Application Load Balancer.
- Copy its DNS name.
- Open the DNS name in your browser.
- Check whether your application loads successfully.
If everything is configured correctly, the load balancer should forward your request to a healthy EC2 instance.
You can also check the target group's health status to confirm that your registered instances are healthy.
If the application does not load, check the listener configuration, security groups, target health, application status, and health check path.
6. What Is Auto Scaling and Why Is It Useful?
So far, we've discussed running an application across multiple EC2 instances.
But what happens when traffic increases?
You could manually launch more instances, but that becomes difficult to manage as your application grows.
This is where EC2 Auto Scaling becomes useful.
Auto Scaling allows you to automatically increase or decrease the number of EC2 instances based on configured conditions.
For example:
- When application demand increases, additional EC2 instances can be launched.
- When demand decreases, instances can be removed.
- Unhealthy instances can be replaced.
You can connect an Auto Scaling group to an Application Load Balancer's target group.
This allows newly launched instances to become registered targets and receive traffic after they pass their health checks.
It helps your application handle changing traffic without requiring you to manage every instance manually.
7. Important Security and Cost Considerations
Deploying an application is only one part of the process. You should also consider security and cost.
Security Best Practices
- Keep application EC2 instances in private subnets.
- Use security groups to restrict unnecessary network access.
- Allow application traffic to EC2 instances only from the load balancer.
- Use IAM roles instead of storing AWS access keys on instances.
- Use HTTPS to protect data in transit.
- Apply operating system and application updates.
- Store sensitive application configuration and credentials securely.
For HTTPS, you can use AWS Certificate Manager (ACM) to manage certificates and configure an HTTPS listener on your Application Load Balancer.
Cost Considerations
Remember that AWS services can generate charges even when you're only experimenting.
Keep these points in mind:
- EC2 instances can incur charges while running.
- Application Load Balancers have hourly and usage-based charges.
- Data transfer can generate additional costs.
- Auto Scaling may launch additional EC2 instances, increasing your bill.
If you're following along in a learning environment, check the pricing before creating resources.
When you're finished experimenting, delete resources you no longer need, including the load balancer, target groups, Auto Scaling group, and EC2 instances. Also check for any remaining resources that may continue generating charges.
8. Common Problems Beginners May Face
When deploying an application with EC2 and an Application Load Balancer, you may encounter a few common issues.
1. Application Not Loading
Possible causes:
- Incorrect listener configuration.
- Security group rules blocking traffic.
- Application service not running.
- Incorrect target group port.
2. Targets Showing as Unhealthy
Possible causes:
- Incorrect health check path.
- Application not responding on the expected port.
- EC2 security group not allowing traffic from the load balancer.
- Application taking too long to respond.
3. Cannot Connect to EC2 Instances
Possible causes:
- Incorrect subnet or routing configuration.
- Missing administrative access permissions.
- Security group restrictions.
- Incorrect connection method.
4. Load Balancer Returns an Error
Possible causes:
- No healthy registered targets.
- Incorrect listener rules.
- Application instances not responding correctly.
When troubleshooting, start by checking target health and security group rules. These are common places to look when an application isn't reachable through a load balancer.
Final Thoughts
Deploying a web application using Amazon EC2 and an Application Load Balancer is an important step in understanding real-world AWS infrastructure.
EC2 provides the computing resources to run your application, while the load balancer distributes incoming traffic across healthy application instances.
By combining these services with VPCs, subnets, security groups, and Auto Scaling, you can build an application environment that is easier to manage, scale, and protect.
You don't need to understand every AWS service before starting. Learn the basic architecture first, then explore each component through practice.
What about you?
Have you deployed a web application on an EC2 instance yet? Have you tried using an Application Load Balancer, or is that something you're planning to explore?
Share your experience in the comments!
Coming Next: Docker to AWS ECS: Deploying Containers Without Managing Servers — exploring how containers can simplify application deployment on AWS.
Series: AWS in the Real World: Building, Securing & Optimizing the Cloud (6 articles)
- …
- 6Deploying a Web Application Using EC2 and Application Load Balancer on AWS This article
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article