A web app deployed with Docker on AWS using Terraform
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.
- 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.
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.
- Containerizing the Web App with Docker: The web app was packaged into a Docker image to ensure it can run consistently across different environments.
- Provisioning AWS Resources with Terraform: Using Terraform, an EC2 instance was provisioned to run the Docker container.
- Deploying the Application: The Docker container is deployed to AWS EC2, allowing it to be accessed via a public IP.
To build and run the Docker container on your local machine:
git clone https://github.com/your-username/docker-aws-web-app.gitNavigate to the directory containing the Dockerfile and run the following command to build the image:
docker build -t my-web-app .Once the image is built, run it with:
docker run -d -p 80:80 my-web-appYou should be able to access the web application at http://localhost on your browser.
Follow these steps to deploy the web application on AWS using Terraform.
Before running Terraform, ensure that AWS credentials are set up:
aws configureOr, set environment variables:
export AWS_ACCESS_KEY_ID=your-access-key
export AWS_SECRET_ACCESS_KEY=your-secret-keyFirst, navigate to the directory containing the Terraform configuration files (main.tf and variables.tf) and initialize Terraform:
terraform initRun the following command to create the resources on AWS:
terraform applyYou’ll be prompted to confirm the actions Terraform will take. Type yes to proceed.
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>
-
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.
Here are some screenshots of the project in action:
-
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.
- Docker Documentation
- Terraform Documentation
- AWS EC2 Documentation
This project is licensed under the MIT License - see the LICENSE file for details.







