Deploy Nginx image by creating an AWS ECS cluster with an EC2 launch type

awsmine

Revathi Joshi

Posted on February 8, 2023

Deploy Nginx image by creating an AWS ECS cluster with an EC2 launch type

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 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

Amazon ECS-optimized AMI

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

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

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


Enter fullscreen mode Exit fullscreen mode
  • launch instance

Amazon ECS-optimized AMI

Image description

  • EC2 Instance - ecs-instances

Image description

  • ECS infrastruture

Image description

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

  • Next, Review and create
  • Create

Image description

  • 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

  • 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

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

Image description

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

  • Networking - Keep default values

Image description

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

Image description

  • create new target group

Image description

  • Create

Image description

  • ALB

Image description

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

Image description

6. Validation

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

Image description

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.

💖 💪 🙅 🚩
awsmine
Revathi Joshi

Posted on February 8, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related