🐳 Deploying a Containerized Web App on AWS using ECS Fargate, ECR, and Load Balancer (Step-by-Step Guide)


Are you curious about how to run your application in the cloud without worrying about managing servers or learning complex tools like Kubernetes? You're in the right place.
In this guide, I’ll walk you through containerizing a simple web app and deploying it on AWS using tools like ECS Fargate, ECR, EC2, and a Load Balancer. Don’t worry if these terms sound unfamiliar—we’ll break them down and make them easy to understand.
By the end of this tutorial, you’ll have a live application running in the cloud with high availability, no server maintenance, and automatic scaling handled for you.
🧰 Key AWS Tools We'll Be Using (Explained Simply)
EC2 (Elastic Compute Cloud): A virtual machine in the cloud. We'll use this to build and test our Docker image before deployment.
Docker: A tool to package our app and its environment into a "container" so it runs the same anywhere.
ECR (Elastic Container Registry): AWS’s private storage for your Docker images. Think of it as your container's home on the cloud.
ECS (Elastic Container Service): AWS’s way of running and managing containers. It works with either your own servers (EC2) or AWS-managed infrastructure (Fargate).
Fargate: A server less option that lets you run containers without managing any servers at all. AWS handles all the heavy lifting.
IAM (Identity and Access Management): Used to create roles and permissions for secure access to services.
Application Load Balancer (ALB): Distributes traffic across multiple instances of your app to ensure availability and prevent downtime.
🚀 What We'll Be Doing
Here’s a high-level view of the steps we’ll follow in this project:
Launch an EC2 instance to install Docker and build our app.
Containerize a simple web application with a Dockerfile and HTML page.
Push the Docker image to AWS ECR.
Set up ECS using Fargate to deploy the container.
Create an Application Load Balancer for routing traffic.
Set up roles and permissions using IAM.
Deploy the app and test it live using your Load Balancer DNS.
Whether you're new to cloud infrastructure or just want a smoother deployment process, this tutorial is for you. Let's get started!
🖥️ Launching an EC2 Instance to Install Docker and Build Our App
Before we containerize our app and push it to the cloud, we’ll need an environment to build and test it. In this case, we’ll use an EC2 instance Amazon’s virtual server—to install Docker and package our application.
Here’s how to get started:
🔧 Step 1: Launch an EC2 Instance
Head over to your AWS Management Console and search for EC2.
Click “Launch Instance”.
Give your instance a name. (I called mine
shepherd
for this project.)Leave most of the default settings as they are.
Under Key Pair (Login), create a new key pair and download the
.pem
file (you’ll use it later to connect).Scroll down to Network Settings and click “Edit”.
Under Security Group, create a new one.
Add an inbound rule for
HTTP
with0.0.0.0/0
as the source to allow web traffic.
Once that’s done, scroll down and click “Launch Instance.”
🧑💻 Step 2: Connect to Your Instance
Once the instance is up and running:
Go to your EC2 dashboard, select your instance, and click “Connect.”
You can use either Instance Connect (opens a browser terminal) or your local SSH client.
For SSH, open your terminal or PowerShell and use the command below:
ssh -i "path-to-your-key.pem" ec2-user@your-ec2-public-ip
🐳 Step 3: Install Docker
Once connected to the instance:
Switch to the root user:
sudo su
Update your packages:
yum update -y
Download the Docker install script:
curl -fsSL https://get.docker.com -o get-docker.sh
Install Docker:
yum install docker -y
Start Docker and confirm it’s running:
systemctl start docker systemctl status docker
You should see a green “active (running)” message. Press
Q
to exit status view.Log into docker
To log in to Docker via the terminal, you can use the following command:
docker login
After running this command, you’ll be prompted to enter your Docker Hub username and password.
🧱 Step 4: Create Your App Files
Create a working directory:
mkdir shepherd-app cd shepherd-app
Create a simple Dockerfile:
vi Dockerfile
Paste the following inside:
FROM almalinux:8 RUN dnf install -y httpd && dnf clean all COPY index.html /var/www/html/ CMD ["/usr/sbin/httpd","-D","FOREGROUND"] EXPOSE 80
- Press
Esc
, then type:wq
to save and exit.
- Press
Create your
index.html
:vi index.html
Before pasting, type:
:set paste
Then paste in your HTML. For testing, a simple message like this is fine:
<h1>Hello!</h1>
Confirm the file was saved:
cat index.html
🧪 Step 5: Build and Run Your Docker Container
Build the Docker image:
docker build . -t dinrei_img
Run the image in a container:
docker run -d -p 80:80 --name dinrei_container dinrei_img
Confirm it’s running:
docker ps
Now, go back to your EC2 dashboard, copy the Public IPv4 address, and paste it in your browser. You should see your custom HTML page displayed!
Copying the IPv4 address should load up your site on your browser
📦 Pushing Your Docker Image to AWS ECR (Elastic Container Registry)
Now that your application is containerized and running on EC2, the next step is to upload your Docker image to Amazon ECR. ECR is AWS’s private Docker image repository—it’s where ECS pulls images from during deployment.
🧭 Step 1: Create an IAM Role and Access Keys
To interact with AWS from your EC2 instance (via the CLI), you'll need to create programmatic credentials.
Go to the IAM service in your AWS Console.
Navigate to Users → select the IAM user created.
Click on “Create access key“
Under the Security credentials tab, click “Create access key.”
The access key will be downloaded it your PC in an excel spreadsheet.
Copy the Access key ID and Secret access key somewhere safe.
🔐 Step 2: Configure the AWS CLI
Back in your EC2 terminal:
aws configure
You’ll be prompted to enter:
Access key ID
Secret access key
Default region name (e.g.
us-east-1
,eu-north-1
, or your specific region)Default output format (you can leave this as
json
or press Enter)
🗂️ Step 3: Create a Repository in ECR
Still on the EC2 terminal, run:
aws ecr create-repository --repository-name dinrei_img
This creates a private repository for your Docker image. You can confirm it on the AWS Console under ECR > Repositories.
🔑 Step 4: Authenticate Docker to ECR
Run this command to log Docker into ECR using your credentials:
aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <your-account-id>.dkr.ecr.<your-region>.amazonaws.com
Replace:
<your-region>
with your AWS region (e.g.eu-north-1
)<your-account-id>
with your AWS account number
🏷️ Step 5: Tag and Push Your Image
First, access your EC2 instance to run the following Docker commands:
Go to the EC2 Dashboard.
Click on the EC2 instance you created.
Click Connect and choose the EC2 Instance Connect (browser-based CLI) option.
Once inside the terminal, run the commands displayed there.
Tag your image so it matches your ECR repository:
docker tag dinrei_img:latest <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/dinrei_img:latest
Push the image to ECR:
docker push <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/shepherd_img:latest
✅ After the push, your image is now in Amazon ECR and ready to be pulled by ECS during deployment.
🔍 Test Your App from EC2 (Optional, but recommended)
Before moving on, grab the public IPv4 address of your EC2 instance, open your browser, and check if your containerized app is still running:
http://<your-public-ip>
If it loads your app, then everything is good.
🌐 Setting Up ECS Fargate to Deploy the Container
Now that your image is stored in ECR, it’s time to deploy using ECS with Fargate — no server management, AWS handles scaling for you.
🛰️ Step 1: Create a Load Balancer
Go to EC2 > Load Balancers.
Click Create Load Balancer.
Select Application Load Balancer.
Name your load balancer — e.g.,
dinrei_load_balancer
.Scheme: Select
Internet-facing
.Network mapping:
VPC: Select the default VPC.
Availability Zones: Select at least two.
Security Groups:
- Select the one created with your EC2 instance — e.g.,
dinrei-wizard-1
.
- Select the one created with your EC2 instance — e.g.,
Listeners and Routing:
Protocol:
HTTP
Port:
80
🎯 Step 2: Create a Target Group
Since no target group exists yet:
On the load balancer creation page, under Target Group, select Create Target Group.
Target type:
Instances
Name: e.g.,
dinrei_target_group
Protocol: HTTP | Port: 80
IP Address type: IPv4
Leave everything else as default and click Next.
Select your running instance and register it.
Complete creation and return to the Load Balancer setup.
Select the newly created target group and finalize creating the Load Balancer.
🔐 Step 3: Create an IAM Role for ECS
Go to IAM > Roles > Create Role.
Trusted entity type: AWS Service.
Use case: Elastic Container Service > Elastic Container Service Task.
Permissions: Add the following policies:
Leave the rest as is and create the role.
🧱 Step 4: Create a Task Definition
Go to ECS > Task Definitions > Create New Task Definition.
Launch Type: AWS Fargate.
Task Name: e.g.,
dinrei-task
.Task Role: Select the IAM role created above.
Go to ECR > Private Registry > Repositories and copy your image URI.
Under Container Definitions, click Add Container:
Name: e.g.,
container-1
Image: Paste the image URI from ECR.
Port mapping: 80
Click Create to finish.
📦 Step 5: Create an ECS Cluster
Go to ECS > Clusters > Create Cluster.
Give it a name — e.g.,
dinrei-cluster
.Leave all other settings as default and create.
⚙️ Step 6: Create a Service
In your ECS cluster, go to the Services tab > Click Create.
Task Definition: Select the one you created (
dinrei-task
).Service type:
Replica
Desired tasks: 2
Turn on Availability Zone Rebalancing.
VPC: Choose the default VPC.
Subnets: Select at least two subnets.
Security group: Choose existing and select your previously used group.
🔗 Load Balancing
Select Use an existing load balancer.
Choose the load balancer you created earlier.
For listeners: HTTP:80
Under Target group:
Select Create new target group.
Name: e.g.,
dinrei-task-group
Protocol: HTTP
Deregistration delay: 300
Evaluation order: 2
Health check protocol: HTTP
Leave the rest as default and click Create.
You’ll now see the service created in your cluster, and it should show your two tasks running.
Based on the images above, all deployments completed successfully.
I then copied the IPv4 address of my EC2 instance, accessed it in my browser, and here’s the result:
I retrieved the DNS of my load balancer and attempted to access it, I encountered an error.
🧪 Step 7: Register the EC2 Instance with Target Group
To fix the issue, we need to register the target group. From the screenshot below you’ll notice the target is zero which is causing this error.
Go to EC2 > Target Groups.
Select the one you created — e.g.,
dinrei-target-group
.Scroll to Registered Targets.
If empty, click Register Target.
Select your running EC2 instance.
Click Include as pending, then Register pending targets.
Confirm that total targets = 1.
✅ Step 8: Verify Deployment
Go to EC2 > Load Balancers.
Select your load balancer and go to Resource Map.
Confirm that your target group is linked.
Scroll to the DNS Name of the load balancer.
Paste it into your browser.
🎉 You should now see your deployed application live on Fargate!
✅ Final Summary
You’ve now successfully:
🐳 Containerized your app and pushed it to ECR
📦 Deployed it to Fargate without managing servers
🌍 Made it publicly accessible through an Application Load Balancer
🔐 Secured it via a security group and public subnet configuration
This is a scalable, production-ready cloud deployment, and you can now easily scale your service by increasing the task count or adding autoscaling rules.
Thanks for checking out my blog post—I hope it helped guide you through the process clearly!
Feel free to like, follow, and drop a comment to share your thoughts or questions about the project.
I’d love to hear your feedback!
Subscribe to my newsletter
Read articles from Di Nrei Alan Lodam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
