Elastic Container Service is a fully managed container orchestration service provided by AWS. It is a choice to run containers on AWS. It supports Fargate to provide serverless compute for containers. Fargate removes the need to provision and manage servers. ECS helps to focus on building and managing applications instead of infrastructure. It is a highly scalable, fast container management service that makes it easy to run, stop, and manage containers on a cluster.

ECS comes with different pricing models. To know about its pricing visit the AWS official page here.

Basic terminologies in ECS.

  1. ECS Cluster: It is a logical grouping of tasks or services.
  2. The Task Definition:  It describes one or more containers (up to a maximum of ten) that form your application.
  3. The Task: It is a runnable unit of a task definition.
  4. Service:  It is used to run and maintain a specified number of instances of a task definition.

In this article, we will create an ECS cluster and deploy a sample Nginx application onto it. We will create a test cluster to understand the cluster setup. You are advised to set up a production cluster with a customized configuration as per the requirement.

Pre-requisites

  1. AWS Account(Create if you don’t have one).
  2. Basic understanding of IAM Role(Click here to learn about IAM). 
  3. Basic understanding of VPC(Click here to learn about VPC)

What will we do?

  1. Login to AWS(Create if you don’t have one).
  2. Create a Cluster
  3. Create a Task Execution IAM Role
  4. Create a Task Definition
  5. Create a Service
  6. Access the Task we created
  7. Delete the Cluster

Login to AWS

Click here to go to the AWS Login page and enter your credentials.

How to setup Elastic Container Service (ECS) on AWS linux

Once you login into the account, you will see the main AWS Management Console as follows.

How to setup Elastic Container Service (ECS) on AWS linux

Create a Cluster

To go to ECS, click on services in the top left of the screen and under Containers, click on Elastic Container Service.

How to setup Elastic Container Service (ECS) on AWS linux

You will see the main dashboard of ECS as below. Click on “Clusters” in the left panel.

How to setup Elastic Container Service (ECS) on AWS linux

Since I have not created any cluster we see nothing in the list. Create your first cluster click on the “Create Cluster” button.

How to setup Elastic Container Service (ECS) on AWS linux

Here, select the “EC2 Linux   Networking” option and click on the “Next step” button.

How to setup Elastic Container Service (ECS) on AWS linux

Give a name to the cluster, select EC2 instance type. EC2 instances in the cluster will get created of the type we select. Select the provisioning model of EC2 instances to be created. Specify other configurations as per your requirement.

How to setup Elastic Container Service (ECS) on AWS linux

You can either create a new PC or choose an existing one. If you are not aware of VPC and networking and would like to know more about it I would recommend you go through the article here.

How to setup Elastic Container Service (ECS) on AWS linux

You can specify tags if you want or you can keep it empty as it is an optional configuration. Click on the “Create” button which will create a cluster for you.

How to setup Elastic Container Service (ECS) on AWS linux

This will take a few minutes and once the cluster has been created you can see the status as  “ECS Status -3 of 3.. “on the same page. Click on the “View Cluster” button to go to the cluster.

How to setup Elastic Container Service (ECS) on AWS linux

Create a Task Execution IAM Role

Before you proceed with the further configuration you will need a role that will be used for task execution. So go to IAM and create a new role with the following policy. Click here if you are not aware of IAM and would like to learn more about it.

Create a Role: ecsTaskExecutionRole with the following policy.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ecr:GetAuthorizationToken",
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "*"
        }
    ]
}

Once you create a Role you will see it as follows in the IAM console.

How to setup Elastic Container Service (ECS) on AWS linux

Create a Task Definition

If you are ready with the IAM Role, you can now proceed with creating a task definition.

Till this point, you just have an empty cluster that does not have your containerized application running into it.

The next step is to create a task definition. To create your first task definition click on “Task Definitions” in the left panel under “Clusters”.

How to setup Elastic Container Service (ECS) on AWS linux

Here click on the “Create new Task Definitions” button.

How to setup Elastic Container Service (ECS) on AWS linux Advertisement

On this page select, the launch type as “EC2 And then click on the “Next” step button”. 

How to setup Elastic Container Service (ECS) on AWS linux

Remember we created an IAM role in the previous step? Now, we need to specify the Role we created under the Task role. Give a name to the task definition.

How to setup Elastic Container Service (ECS) on AWS linux

When you scroll down a bit you will see the “Add container” button, click on it and you will see a new screen popping up as follows.

On this screen given a name to the container, specify the image to be used for the container. You can also specify memory limits, port mappings, health checks. If you’re done with the configuration click on the “Add” button.

How to setup Elastic Container Service (ECS) on AWS linux

Specify “Task memory” and “Task CPU” that will be used by the task.

How to setup Elastic Container Service (ECS) on AWS linux

Now you are ready with the basic and mandatory configuration for the task, click on the “Create” button which will create a task definition.

How to setup Elastic Container Service (ECS) on AWS linux

You will see the message as “Created Task Definition successfully”.

How to setup Elastic Container Service (ECS) on AWS linux

Create a Service

Till this point, we have a cluster and task definition. The next step is to create a service. Go back to the cluster we created and under the Service tab click on the Create button to create a Service.

How to setup Elastic Container Service (ECS) on AWS linux

Choose the launch type as EC2,  select the task definition we created, you can choose the Cluster name from the drop-down option, Even name to the service we are creating, specify the number of tasks as 2 which will create 2 copies of your application.

How to setup Elastic Container Service (ECS) on AWS linux

Keep the rest of the configuration as is under deployments and task placement. Tags are optional so it is not mandatory to specify tags. Click on the “Next step” button to proceed further.

How to setup Elastic Container Service (ECS) on AWS linux

If you have any load balancer then you can select it to route the external traffic to your application through it. Click on the “Next step” button.

How to setup Elastic Container Service (ECS) on AWS linux

If you do not want your service to be scaled then proceed with the default configuration as “Do not adjust the service’s desired count”.

How to setup Elastic Container Service (ECS) on AWS linux

Review the configuration that we have just made for the service to be created click on the “Create Service” button.

How to setup Elastic Container Service (ECS) on AWS linux

You see the status as “ECS Service status- 1 of 1 completed” if the service has been created successfully.

How to setup Elastic Container Service (ECS) on AWS linux

Access the Task we created

Go back to the cluster and under the “Tasks” tab now you can see 2 tasks are running. These tasks contain your application.

Click on one of the tasks to know more about it.

How to setup Elastic Container Service (ECS) on AWS linux

Since we have not specified any load balancer we cannot access the application from the external world on a specific link. Because of this, we would need to access our tasks on their individual external links.

Under Network bindings, you can find the external link of the task. This particular task will only be accessible from the link available here. Copy the link to test if the application, Nginx, that we had specified in the task definition is up and running.

How to setup Elastic Container Service (ECS) on AWS linux

Go to the browser and hit the link we copied in the previous step. You can see the default page of Nginx. This means our application, Nginx, has been successfully deployed. 

How to setup Elastic Container Service (ECS) on AWS linux

Delete the Cluster

If you no longer need your cluster you can delete it with a single click. Go back to the cluster and click on the Delete Cluster” button.

Confirm the deletion action by entering “delete me” in the text field and click on the “Delete” button. This will permanently delete the cluster and once deleted you can not recover it. So be careful while deleting the cluster if you are working on real-time applications.

How to setup Elastic Container Service (ECS) on AWS linux

Conclusion

In this article, we saw the steps to create a cluster, task definition and a service. The steps we saw can be used to create a test cluster. If you want to create a Cluster with the real-time application, you need to be more specific about the configurations we saw.