DEV Community

Abdiel Jonathan
Abdiel Jonathan

Posted on

Deploying a Secure & Scalable Web App on AWS EC2 (No ECS, No Beanstalk!)

Starting the Tutorial

In this tutorial, we’ll go step-by-step through deploying a Dockerized web application on AWS EC2 using only EC2 services — no Elastic Beanstalk, no ECS, no shortcuts.

By the end, you’ll have:

  • A launch template that auto-installs Docker and deploys your app.
  • An Auto Scaling Group for high availability.
  • A custom domain pointing to your EC2 instance.

Prerequisites
Before we start, make sure you have:

  • An AWS account
  • Access to ChatGPT (optional, but great for debugging along the way!)
  • IAM roles with permissions for EC2, ECR, SSM, and Auto Scaling
  • Basic knowledge of GitHub (to clone your project repo)
  • Docker installed on your local machine

Part 1 – Configuring EC2 Using a Launch Template

We’ll start by setting up an EC2 Launch Template — this defines how each instance should be created, configured, and secured.
a - On your AWS Management Console, search for “Launch Templates”.


b - We will then be redirected to this page and proceed to click on Create Launch Template


You’ll now see a form to define the template’s details. Let’s fill it out together 👇

Basic Configuration

  1. Template name: cloud-tutorial

  1. Application and OS Image (AMI):
  • Ubuntu Server 24.04 LTS (64-bit x86)

  1. Instance type: t3.small

  1. Network settings:
  • Create a new security group

  • Allow port 80 (HTTP) and port 443 (HTTPS)
  • Disable SSH access — we’ll use SSM Session Manager for secure logins.

  1. Storage: 20 GB (change from the default 8 GB)

  2. Monitoring: Enable CloudWatch monitoring

This helps with debugging and tracking CPU, disk I/O, and network usage.

  1. IAM instance profile:

Choose your IAM role (e.g., LabRole if you’re using AWS Academy).

Finally, click Create launch template ✅

You should now see your new template listed.

Part 2 - Setting Up Auto Scaling for High Availability

Next, we’ll configure an Auto Scaling Group (ASG) to automatically manage EC2 instances.

  1. Go to EC2 → Auto Scaling Groups → Create Auto Scaling Group

  1. Name it ASG-cloud-tutorial
  2. Select your launch template (cloud-tutorial)
  3. Choose two Availability Zones and their subnets for high availability.(In the network section)

  • Leave the distribution as “Balanced best effort”.
  • Integrating Load Balancing In the “Load Balancing” section:
    • Enable Elastic Load Balancing (ALB).

* Turn on ELB health checks, so EC2 Auto Scaling can replace unhealthy instances.(Then configure scaling:Minimum capacity: 1,Desired capacity: 1,Maximum capacity: 3,Target tracking policy: Average CPU utilization at 50%.)
  This ensures your app scales up under load and back down when idle — saving costs while staying responsive.
Enter fullscreen mode Exit fullscreen mode


Once done, click Create Auto Scaling Group ✅
Your configuration should look like this:
Part a -

Part b -

Part c -

Part 3 – Deploying the Application

Now that your infrastructure is ready, let’s deploy a simple app.
Project Structure
Your app repository will look like this:

Deployment steps

  1. Clone your repo to your local machine:
git clone https://github.com/AbdielJonathan007/cloud-tutorial
cd cloud-tutorial

Enter fullscreen mode Exit fullscreen mode
  1. Use Docker Buildx for multi-platform builds (for M1/M2 Mac compatibility and when we upload to EC2 instances later):

  1. Create an ECR repository for each component:
    • backend
    • frontend
    • nginx At Amazon search bar look for : ECR repository Choose: Visibility: Private Repository name: hello-world-app Leave encryption and scanning default Click Create repository After creating you get a url like this:339712882354.dkr.ecr.us-east-1.amazonaws.com/hello-world-app
    • After creating the repository we will need the following information

  • Which in our case we are using the canvas lab to access the information would be under AWS details then clicking on AWS CLI , then we fill out that informationB Then we run this command to Retrieve an authentication token and authenticate your Docker client to your registry. Use the AWS CLI:
    • Then we run this command
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin 339712882354.dkr.ecr.us-east-1.amazonaws.com

Enter fullscreen mode Exit fullscreen mode

We will need to push 3 Docker images

  • frontend

  • backend

  • nginx

Part 4 - Automating Deployment via Launch Template

Now we’ll make sure new EC2 instances automatically install Docker and run the app.
Steps:

  1. Go to EC2 → Launch Templates → Actions → Create new version
  2. Scroll to Advanced details → User data
  3. Paste this script:
#!/bin/bash
apt update -y
apt install -y docker.io unzip curl
systemctl enable docker --now
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin 339712882354.dkr.ecr.us-east-1.amazonaws.com

docker pull 339712882354.dkr.ecr.us-east-1.amazonaws.com/backend:latest
docker pull 339712882354.dkr.ecr.us-east-1.amazonaws.com/frontend:latest
docker pull 339712882354.dkr.ecr.us-east-1.amazonaws.com/nginx:latest

docker network create myapp-network
docker run -d --name backend --network myapp-network -p 5000:5000 backend:latest
docker run -d --name frontend --network myapp-network -p 3000:3000 frontend:latest
docker run -d --name nginx --network myapp-network -p 80:80 nginx:latest

Enter fullscreen mode Exit fullscreen mode

4 - Create the new version and attach it to your Auto Scaling Group (ASG).
Now, whenever ASG launches a new EC2 instance, it’ll run this script automatically. 💪
We should see something like this :

Part 5 - Connecting Your Domain (Using Name.com Only)

Now that our app is up and running on EC2, let’s make it accessible through a custom domain instead of the public IP.

We’ll be using a domain from Name.com, and we’ll connect it directly using DNS “A” records — no Route 53 needed.

Step 1: Get Your EC2 Public IP

1️⃣ Open the AWS Management Console → EC2 → Instances.
2️⃣ Select the instance that’s running your app.
3️⃣ Copy the Public IPv4 address (for example, 3.90.248.104).

Step 2: Add DNS Records on Name.com

1️⃣ Log in to your Name.com
account.
2️⃣ Go to My Domains → abdielcloud.live → Manage DNS.
3️⃣ Under DNS Records, add two A records:


Click Save Changes when you’re done.

Once propagation finishes, navigating to
http://abdielcloud.live
(or http://www.abdielcloud.live)
should open your frontend running on the EC2 instance 🎉

Thank you for reading thus far!

Top comments (0)