Skip to content

JasonAkrofi/docker-aws-web-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

docker-aws-web-app

A web app deployed with Docker on AWS using Terraform

Web App Deployment with Docker & AWS

This project demonstrates how to deploy a simple web application using Docker and AWS EC2, managed through Terraform. The goal is to understand how Docker containers, cloud infrastructure (AWS), and Infrastructure-as-Code (Terraform) work together for automating deployment and managing resources.


Technologies Used

  • AWS EC2 (Elastic Compute Cloud) - For creating and managing virtual servers in the cloud.
  • Docker - For containerizing the web application.
  • Terraform - For Infrastructure-as-Code (IaC) to define and provision AWS resources.
  • Nginx - A lightweight web server to serve the application.

Project Overview

This project covers the process of deploying a basic web application (using Docker) to an EC2 instance on AWS. The entire deployment is automated using Terraform scripts.

Key Steps:

  1. Containerizing the Web App with Docker: The web app was packaged into a Docker image to ensure it can run consistently across different environments.
  2. Provisioning AWS Resources with Terraform: Using Terraform, an EC2 instance was provisioned to run the Docker container.
  3. Deploying the Application: The Docker container is deployed to AWS EC2, allowing it to be accessed via a public IP.

How to Run Locally

To build and run the Docker container on your local machine:

Step 1: Clone this repository

git clone https://github.com/your-username/docker-aws-web-app.git

Step 2: Build the Docker image

Navigate to the directory containing the Dockerfile and run the following command to build the image:

docker build -t my-web-app .

Step 3: Run the Docker container

Once the image is built, run it with:

docker run -d -p 80:80 my-web-app

You should be able to access the web application at http://localhost on your browser.


How to Deploy on AWS with Terraform

Follow these steps to deploy the web application on AWS using Terraform.

Step 1: Configure AWS Credentials

Before running Terraform, ensure that AWS credentials are set up:

aws configure

Or, set environment variables:

export AWS_ACCESS_KEY_ID=your-access-key
export AWS_SECRET_ACCESS_KEY=your-secret-key

Step 2: Initialize Terraform

First, navigate to the directory containing the Terraform configuration files (main.tf and variables.tf) and initialize Terraform:

terraform init

Step 3: Apply Terraform Configuration

Run the following command to create the resources on AWS:

terraform apply

You’ll be prompted to confirm the actions Terraform will take. Type yes to proceed.

Step 4: Access the Application

Once Terraform completes the deployment, it will output the public IP of the EC2 instance. Use this IP to access the web application in your browser. For example:

http://<EC2_PUBLIC_IP>

Security Best Practices

  • Use IAM Roles instead of hardcoded credentials.

  • Restrict Security Groups: Avoid opening all ports to the public.

  • Enable Logging & Monitoring: Use AWS CloudWatch for monitoring.


Screenshots

Here are some screenshots of the project in action:

1. EC2 Dashboard

Screenshot 2025-03-02 191604

2.Web App Running on EC2

Screenshot 2025-03-02 191604

3. Project Structure

project folder

4. Docker Image & Container

docker image   container

5. AWS ECR Repository

aws ecr repository

6. Terraform Execution

terraform execution

7. AWS EC2 Instance

Screenshot 2025-03-02 191604

8. Running Web App

web app


Troubleshooting

  • EC2 Instance Not Starting: Ensure that the AMI ID is correct for the AWS region you are deploying to. You can verify the correct AMI ID in the AWS EC2 Console.

  • Terraform Authentication Issues: Ensure AWS credentials are properly configured (aws configure).

  • Docker Container Not Starting: Ensure Docker is correctly installed and running on your local machine.

  • Public IP Not Accessible: Check security group rules to allow HTTP (port 80) traffic.


Resources

  • Docker Documentation
  • Terraform Documentation
  • AWS EC2 Documentation

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A web app deployed with Docker on AWS using Terraform

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published