Deploying web applications on AWS ECS (Elastic Container Service) with a Load Balancer is a powerful way to achieve high availability and scalability for your web projects. In this tutorial, we’ll go step-by-step through the process of containerizing an application with Nginx, pushing it to Amazon ECR (Elastic Container Registry), and deploying it using AWS ECS with a Load Balancer.
Step 1: Setting Up Your Web Application
To get started, we need a simple web application. Let’s create a straightforward HTML file named index.html to serve as the content for our Nginx server. Here’s a basic example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Hello World</title> <!-- Bootstrap CSS --> <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container text-center"> <div class="row"> <div class="col"> <h1 class="display-4 mt-5">Hello, World from ECS!</h1> <p class="lead">This page is powered by Nginx running on Amazon ECS.</p> </div> </div> </div> <!-- Bootstrap JS and dependencies --> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </body> </html> |
This HTML file will be displayed as a “Hello World” page once the deployment is complete.
Step 2: Dockerize the Application
To host this application on ECS, we need to containerize it using Docker. First, create a Dockerfile in your project’s root directory with the following content:
1 2 |
FROM nginx:alpine COPY index.html /usr/share/nginx/html/index.html |
This Dockerfile copies index.html into the default web directory of an Nginx container.
Build and Test the Docker Image Locally
Now, let’s build and test the Docker image locally before pushing it to AWS:
1 |
docker build --platform linux/amd64 -t nginx-hello-world . |
Test locally
1 |
docker run -d -p 8080:80 nginx-hello-world |
Visit http://localhost:8080 in your browser to see if your “Hello, World” page is served correctly.
Step 3: Push the Docker Image to Amazon ECR
With your Docker image tested and ready, the next step is to push it to Amazon ECR. Start by creating a repository in ECR through the AWS Console, and follow these commands to log in and push your image:
At the top of the repository page, you’ll see the View push commands button. Click it to see the steps to push your Docker image to ECR.
Go to the command line and run these commands:
1 |
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 557310840278.dkr.ecr.us-east-1.amazonaws.com |
1 |
docker build --platform linux/amd64 -t nginx-hello-world . |
1 |
docker tag nginx-hello-world:latest 557310840278.dkr.ecr.us-east-1.amazonaws.com/nginx-hello-world:latest |
1 |
docker push 557310840278.dkr.ecr.us-east-1.amazonaws.com/nginx-hello-world:latest |
Step 4: Create an ECS Cluster and Task Definition
Navigate to the ECS section in the AWS Console and create a new cluster. Choose the appropriate settings (such as Fargate or EC2 launch type) based on your requirements.
After creating the cluster, define a new ECS Task Definition. Specify the container details such as container name, image URI from ECR, memory limits, and port mappings (e.g., 80 for Nginx). This task definition will be the blueprint ECS uses to deploy and run the container.
Next up we need to create the task definition.
Add a container:
Next, let’s create a service within that Cluster