Deploy Nginx image by creating an AWS ECS cluster with an EC2 launch type
Tags: ecs, ec2, alb, nginx
Published: 2023-02-08 02:29:39

In this article, I am going to show you how to create an AWS ECS cluster with the EC2 launch type, spreading the EC2 instance across all availability zones, providing high availability, which is accessible from the Application Load Balancer.

**Amazon Elastic Container Service (Amazon ECS)**
- It is a highly scalable and fast container management service. 

- You can use it to run, stop, and manage containers on a cluster. 

- An Amazon ECS cluster is a logical grouping of tasks or services and includes 3 main components Clusters, Tasks and services.

- With Amazon ECS, your containers are defined in a task definition that you use to run an individual task or task within a service. 

- In this context, a service is a configuration that you can use to run and maintain a specified number of tasks simultaneously in a cluster. 

- You can run your tasks and services on a serverless infrastructure that's managed by AWS Fargate. 

- Alternatively, for more control over your infrastructure, you can run your tasks and services on a cluster of Amazon EC2 instances that you manage.

- Clusters are AWS Region specific.

Please visit my **[GitHub Repository for Docker articles](https://github.com/awsmine/Docker_projects)** on various topics being updated on constant basis.

_**Let’s get started!**_


# Objectives:

**1.** Create role for EC2 instance in order to be able to connect to ECS Cluster

**2.** Create ECS Cluster

**3.** Create an EC2 Instance which acts as a Container host

**4.** Create task definition and Task, and Stop the Task

**5.** Create a Service with the ALB

**6.** Validation


# Pre-requisites:
- AWS user account with admin access, not a root account.


# Resources Used:

**[Amazon Elastic Container Service](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/Welcome.html)**

**[Amazon ECS-optimized AMI](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html)**


# Steps for implementation to this project:


## 1. Create role for EC2 instances in order to be able to connect to ECS Cluster

- **`On IAM dashboard, Create role, Use case - EC2, Next, select AmazonEC2ContainerServiceforEC2Role, Next, ecsinstancerole, Createrole`**

- Create

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


## 2. Create ECS Cluster

- **`On Amazon Elastic Container Service Dashboard, Create Cluster, myecs-cluster, default vpc, all public subnets, External instances using ECS Anywhere`**

> ECS Anywhere will allow you to run your containers managed by AWS outside of AWS

- Create


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


## 3. Create an EC2 Instance which acts as a Container host

- **`Go to EC2 Dashboard, Launch instance, ecs-instances, Under Application and OS Images (Amazon Machine Image), search for Amazon ECS-optimized AMI, Select, Continue, t2.micro. NVirKey, default vpc, any public subnet, Create Security group, SSH with 0.0.0.0/0, Under Advanced Details, select ecsinstancerole`**

**user data for configure cluster**

```
#!/bin/bash
echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.config

#!/bin/bash
echo ECS_CLUSTER=myecs-cluster >> /etc/ecs/ecs.config
```

- launch instance

**Amazon ECS-optimized AMI**


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


- EC2 Instance - **`ecs-instances`**



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



- ECS infrastruture



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



## 4. Create task definition and Task, and Stop the Task

- launch a task on our cluster 
 
- the container is going to pull back the nginx image, which is a simple web service. 

- **`on ECS Dashboard, Task Definition, Create a new task definition, my-nginx, Under Container - 1, Name - nginx, Image URI - nginx:latest`**

- Next

- Use the following values


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


- Next, Review and create
- Create


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


- **`on ecs-cluster, Tasks, run new task, Under Compute configuration, Launch type - EC2, Check Task, Family - my-nginx, Revision - 2(LATEST), default vpc, all public subnets, default security groups with HTTP, Port 80`** 

- Create


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

- Click on the Task

- there is no Public IP, so no way to connect
- because with the EC2-Launch type using network mode as awsvpc, there will not be any Public IP


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


- so Go over to Tasks, click the task, stop the task


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


## 5. Create a Service with the ALB

- **`on ecs-cluster, Services, Create, Under Compute configuration, Launch type - EC2, Check Service, Family - my-nginx, Revision - 2(LATEST), Service name - my-ecs-service`**



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

- Networking - Keep default values


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


- Create a Application Load Balancer (ALB) with the following parameters


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


- create new target group


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


- Create


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


- ALB


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




- **`On default VPC, Security Group, Add Rule - HTTP Port 80, Anywhere IvP4 0.0.0.0/0`**

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




## 6. Validation

- Open a new browser and paste the DNS name of ALB to see nginx image

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


# What we have done so far

We have successfully demonstrated how to create an AWS ECS cluster with the EC2 launch type, spreading the EC2 instance across all availability zones, providing high availability, which is accessible from the Application Load Balancer.