DEV Community

Cover image for Step-by-Step Guide: Deploy a Containerized App to AWS
Ijay
Ijay

Posted on

Step-by-Step Guide: Deploy a Containerized App to AWS

This guide explains the steps I followed to containerize an application and deploy it to AWS using Docker, Terraform, ECS Fargate, and GitHub Actions.

The goal is simple.
Build the app, package it in Docker, store the image in ECR, run it on ECS Fargate, and automate the deployment with GitHub Actions.

Let's get started

1. Prepare the Application

Create a simple Node.js application.

Example project structure:

app/
server.js
package.json
Dockerfile
terraform/
Enter fullscreen mode Exit fullscreen mode

Install dependencies:

npm install
Enter fullscreen mode Exit fullscreen mode

Run the application locally to confirm it works.

node server.js
Enter fullscreen mode Exit fullscreen mode

Open:

http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

2. Containerize the Application

Create a Dockerfile.

FROM node:20-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install --production

COPY . .

EXPOSE 3000

CMD ["node", "app/server.js"]
Enter fullscreen mode Exit fullscreen mode

Build the image:

docker build -t job-tracker-app .
Enter fullscreen mode Exit fullscreen mode

Run the container locally:

docker run -p 3000:3000 job-tracker-app
Enter fullscreen mode Exit fullscreen mode

Open the browser:

http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

Application on local

3. Create an Amazon ECR Repository

Open AWS Console.

Go to:

ECR → Create repository

Give it a name:

job-tracker-app
Enter fullscreen mode Exit fullscreen mode

After creating the repository, push your Docker image.

Login to ECR:

aws ecr get-login-password --region us-east-1 \
| docker login --username AWS --password-stdin ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com
Enter fullscreen mode Exit fullscreen mode

Tag the image:

docker tag job-tracker-app:latest ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/job-tracker-app:latest
Enter fullscreen mode Exit fullscreen mode

Push the image:

docker push ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/job-tracker-app:latest
Enter fullscreen mode Exit fullscreen mode

AWS ECR

4. Provision Infrastructure with Terraform

Create infrastructure using Terraform.

Resources created include:

  • VPC
  • Public subnets
  • Security groups
  • ECS cluster
  • ECS service
  • Application Load Balancer

Initialize Terraform:

terraform init
Enter fullscreen mode Exit fullscreen mode

Apply infrastructure:

terraform apply
Enter fullscreen mode Exit fullscreen mode

Confirm the ECS service is running.

You should see:

1/1 tasks running
Enter fullscreen mode Exit fullscreen mode

ECS

5. Verify the Application

Once the ECS task is running, the application can be accessed using the public IP assigned to the service.

Open the IP address in the browser to confirm the application is working.


Application on AWS

6. Push the Project to GitHub

Initialize git:

git init
git add .
git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

Create a repository on GitHub.

Connect the repo:

git remote add origin YOUR_REPO_URL
git branch -M main
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

7. Add GitHub Actions for CI/CD

Create this folder in your repository:

.github/workflows
Enter fullscreen mode Exit fullscreen mode

Create a file:

deploy.yml
Enter fullscreen mode Exit fullscreen mode

The workflow should:

  • build the Docker image
  • push the image to ECR
  • update the ECS service

8. Add AWS Credentials to GitHub

In your GitHub repository, go to:

Settings → Secrets → Actions

Add these secrets:

AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
Enter fullscreen mode Exit fullscreen mode

These allow GitHub Actions to deploy to AWS.

9. Trigger Deployment

Push a change to the repository:

git push
Enter fullscreen mode Exit fullscreen mode

GitHub Actions will start the pipeline.

The pipeline will:

  • build the Docker image
  • push it to ECR
  • update the ECS service

Your application will be redeployed automatically.

Final Result

You now have:

  • A containerized application.
  • Infrastructure created with Terraform.
  • Containers running on ECS Fargate.
  • Automated deployment using GitHub Actions.

This setup removes manual deployment and keeps the application updated whenever code changes.

If you found this article helpful, share it with others who may find it interesting.


Other Helpful Resources


Stay updated with my projects by following me on Twitter, LinkedIn, and GitHub.

Thank you for reading

Top comments (0)