Deploy NGINX Application on AWS ECS
Tags: ecs, containers, aws, cluster
Published: 2023-12-28 20:17:07

AWS ECS on Fargate provides a seamless platform to deploy containerized applications with ease and efficiency. In this post, we'll walk through the process of deploying an NGINX application on AWS ECS using Fargate.

## What is AWS ECS and Fargate?

AWS ECS (Elastic Container Service) is a highly scalable, high-performance container management service that supports Docker containers and allows you to run applications on a managed cluster of servers. Fargate, on the other hand, is a serverless compute engine for containers that work with ECS, removing the need to manage servers or clusters. Additionally, you can deploy ECS on an EC2 instance, however you would need to manually manage the servers or cluster.

**Why are we deploying NGINX?**
NGINX is a popular open-source web server known for its high performance, stability, and rich feature set. It's often used for load balancing, reverse proxying, and as a web server.

**Prerequisites**

- An AWS account with permission to use ECS
- AWS CLI installed
- Basic understanding of Docker, NGINX, and AWS AWS ECS

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5c81v6gdomkoulewt5q2.png)


## Step 1: Create an ECS Cluster:
_A cluster is a logical grouping of your ECS resources, you would need a cluster to deploy a container. in this post, we will be using the default vpc for the cluster_

To create a cluster Through AWS CLI 
`aws ecs create-cluster --cluster-name nginx-application`

**To Create a cluster via AWS Console**

> Log in to your AWS Management Console, Navigate to the ECS service, and select “Clusters”.
> Create a new cluster

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/po814hke02t8hhwz2951.png)

## Step 2: Register task definition:
_A Task Definition in AWS ECS is a blueprint for running containers, specifying details like container images, CPU and memory allocation, environment variables, and networking settings._

> **To Create a task definition via AWS Console**
> - **Go to the Task Definitions page** on the ECS console.
> - **Create a new task definition** and select “Fargate” as the launch type.
> - **Configure the task** with the required settings like task size (CPU and memory).
> - **Add a container definition** for your NGINX server. Here, you'll specify the image for NGINX (you can use the official NGINX image from Docker Hub).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dc95sm6u2wwiuvrnimzw.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cuf5u066ni8jtpd2ty4v.png)

## Step 3: Configure and Launch the Service
_A service in AWS ECS creates a task by instantiating a task definition, where a task represents a running container. The service is responsible for managing and maintaining the container's desired state and scalability._

> **To Create a Service via AWS Console**
> - **Return to your ECS cluster** and select “Services”.
> - **Create a new service** and select the task definition you created.
> - **Configure the service** - set the number of desired tasks, and network and security settings.
> - **Launch the service**. AWS Fargate will start and manage the containers for you.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xiaxnz6ea2pr1gbh27t1.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3r4f6mma1et0ijcjewzr.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2e0b021h748rcafkm54p.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tt42bbbrbgniq01xoyys.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3da6jkszlantpsxqem50.png)



## Step 4: Access the NGINX Application
- Find the public IP of your running task (available in the task details in the ECS console).
- Access NGINX by entering the public IP in your web browser. You should see the NGINX welcome page.