In this blog post , We will see how to Launch an ECS cluster using AWS Console and then we will deploy the containerized applications in it.

What is ECS?

Amazon Elastic Container Service is a fast and highly scalable container management service , Using which we can easily run , stop and manage docker containers on the cluster.

Amazon ECS makes it easy to deploy , manage and scale docker containers using applications, services and batch processes.

Amazon ECS can be integrated with IAM Roles , Elastic Loadbalancing , EBS Volumes and Security groups.

ECS Cluster can be hosted in two ways:

  • We can host ECS cluster on a Serverless infrastructure which can be managed by Amazon ECS by launching Tasks and Services using Fargate launch type.
  • We can host ECS cluster and run the tasks on the EC2 instances , By doing so we can get full control over the Infrastructure.

Features of ECS

We can create ECS cluster in a new or existing VPC.

ECS is a regional service which runs applications in a highly available manner across multiple availability zones.

TASK DEFINITIONS:

  • To run the application on the ECS cluster , We create a Task Definition.
  • The Task definition is written in JSON format where we will describe about the containers
  • In a task definition , We can describe maximum of 10 containers.
  • Each task definition will have the following details : Launch type (EC2 or Fargate) , Container Image , Ports to be allowed for applications , Volume mounts.

CONTAINERS & IMAGES:

  • To run applications in the ECS cluster , the application component should be architected to run in containers.
  • Docker containers are the standardized unit of software development , which will have information to run applications such as code , libraries , run time and system tools.
  • Containers are created from a read-only template called Image.
  • Docker images are built using Dockerfile.Dockerfile is a plain text file that specifies all the components that are included in the container.
  • The docker images are stored in the registry (such as Docker Registry , Elastic container registry) , These images will be referred in the Task definition and the application will be deployed as containers in the cluster.

TASKS & SCHEDULING:

  • A task is the initiation of the task definition within a cluster.
  • We can specify the number of tasks that should run on the ECS cluster.
  • The ECS task scheduler is responsible for placing tasks within the ECS cluster.
  • We can define a service that runs and maintains specified number of tasks simultaneously.

CLUSTERS:

  • Cluster is a logical group of resources where we run the tasks on the ECS cluster.
  • If we use Fargate launch type for ECS cluster , Amazon ECS will manage cluster resources.
  • If we use EC2 launch type for ECS cluster , The ECS Container instances should be managed by ourself.
  • The ECS container instances are the ECS instances which has amazon ECS container agents running.

CONTAINER AGENT:

  • The container agent run on all the resources within the ECS cluster.
  • These container agent sends the information about the current running tasks , resources utilization etc to the Amazon ECS.
  • These tasks will be handled by Amazon ECS .

Launching ECS Cluster using AWS Console

ECS cluster can launched in two ways.

  • Using AWS Console
  • Using ECS CLI

In this guide , We are going to launch ECS cluster using AWS console.

Login to ECS console.

ECS objects are described in a diagram.

ECS Cluster

In the navigation pane , Choose Clusters

We can either choose Fargate launch type or EC2 launch type.

If Fargate launch type –> Choose Networking Only

If EC2 Instance type –> Choose EC2 Linux+ Networking or EC2 Windows + Networking.

For this guide , I choose EC2 Linux + Networking.

ECS Cluster

and click Next step

Provide a name for the Cluster.

ECS Cluster

Under Instance configuration , Choose the Provisioning model

To save costs , Choose Spot instances.

ECS Cluster

Then Select the EC2 Instance type to be used by the Cluster container Instances.

The number to be used within the Cluster.

For EC2 Ami id , Select the Amazon Linux 1 or Amazon Linux 2.

The Root EBS Volume size to be attached with each of the EC2 instances within the Cluster.

Choose the existing key , If not create a key pair we should use it here for SSH into the container instances.

ECS Cluster
ECS Cluster

For Networking , We can create a new VPC or we can use the existing VPC to launch ECS cluster.

Choose the VPC , Select the Subnets , Choose the existing Security group or create a new one.

Also allow the inbound rules accordingly.

ECS Cluster

For Container Instance IAM role , The amazon ECS container agents (installed in the container instances ) will make API calls to ECS API actions.

Hence the container instances that run container agents will require ecsInstanceRole IAM policy and the role for the service to know that the agent belongs to ECS.

We can attach the existing Role or we can let it create on the go.

ECS Cluster

For monitoring & troubleshooting purpose , We can enable Cloudwatch container insights for applications running as a container within the cluster.

ECS Cluster

And click Create.

ECS Cluster

And the ECS Cluster is successfully created.

Cloudformation stack is used to launch the resources on our behalf.

ECS Cluster

Click View Cluster to check the available / running ECS cluster.

Click ECS Instances , To check the status of the ECS container instances.

ECS Cluster

And we can see that the Instance doesn’t have any running tasks.

ECS Cluster

Creating Task Definition

Lets setup a Task definition and run sample applications.

In the navigation pane , Choose Task Definitions , Lets create a new task definition.

Click Create new Task Definition

Select the Launch type as EC2 and click Next

ECS Cluster

Provide a name for the task definition,

ECS Cluster

We can Optionally create and assign an IAM role if the ECS cluster instances want to communicate with other AWS services such as RDS , Elasticache Redis etc.

For Network Mode , We have to choose the Docker networking mode for the containers in the task.

Lets use the default networking mode.

Scroll Down , Under Volumes ,Click Configure via JSON and paste the below configuration.

{
"containerDefinitions": [
{
"entryPoint": [
"sh",
"-c"
],
"portMappings": [
{
"hostPort": 80,
"protocol": "tcp",
"containerPort": 80
}
],
"command": [
"/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\""
],
"cpu": 10,
"memory": 300,
"image": "httpd:2.4",
"name": "simple-app"
}
],
"family": "console-sample-app-static"
}

and click Save and click Create.

Creating Service

Amazon ECS service enables us to run and maintain a specified number of instances of a task definition simultaneously in an ECS cluster.

If any task fails or stops for any reason , Then the ECS Service scheduler launches another instance of task definition to replace it in order to maintain the desired number of tasks in the service.

To create a Service , Select the cluster , Choose Services and click Create

Choose the launch type as EC2 and select the task Definition which you have created in the previous step.

ECS Cluster

Choose the Cluster and provide a name for the Service.

ECS Cluster

For Service type , Replica will maintain the desired number of tasks across the cluster.

Daemon , will maintain one copy of the task in each of the container instances.

Give the desired number of tasks If you have chosen Replica.

ECS Cluster

Use the default values for the rest of the settings and click Next step

In the Configure network page , leave the default settings and click Next step

We can optionally setup the autoscaling of services based on the cloudwatch alarms.

Click Next step and Choose Create Service

ECS Cluster

To check the status of the service , Click View Service

The Task should be in Running state If everything is set properly.

Click the task , Under the Container sections , Expand the container details , In the Network bindings sections , You can find the public IPv4 Address.

Type the IPv4 address in the web browser and should be see the web page.

ECS Cluster

Conclusion

We have successfuly launched an ECS cluster of EC2 launch type and we tested with sample application.

Hope you find it helpful.Please do check out my other articles.