AWS ECS & Fargate - How to deploy docker container

When developers are working to containerize an application, there are several steps involved. Getting the source code ready is merely half the job done. To package it in a container and get it into a runnable state, a developer will need to build a Dockerfile, which will contain all the necessary packages, runtimes, and dependencies, in addition to the application source code.

Once the Dockerfile has been successfully built, the resulting container image will have to be published in a central image registry, which can be either local or online. Once the image has been published in a registry, it can then be downloaded to a system and launched in the form of a container.

Additionally, there are several points that need to be considered when managing such an infrastructure locally, such as ensuring network communication between containers, provisioning persistent storage, pushing, pulling, copying images from one environment to another, and managing image registries. All these factors add complexity to the development process and can increase the deployment time of the application.

In this guide we will take a look on how to use AWS Fargate Service to deploy a docker container.

Table of Content

  1. Getting started with AWS ECS and Fargate
  2. Prerequisites
  3. Create IAM task execution role for ECS
  4. Creating an ECS Cluster
  5. Creating Task definitions
  6. Running a Task
  7. Creating a new task revision
  8. Creating an ECS Service
  9. Conclusion


1. Getting started with AWS ECS and Fargate

When working with ECS and Fargate, there are four main components that we’ll need to understand.

  1. ECS cluster
  2. Task definitions
  3. Tasks
  4. ECS services

2. Prerequisites

As a primary requirement you only need AWS free tier account for this tutorial.

3. Create IAM task execution role for ECS

  1. Go to the IAM console, select Roles from the navigation menu on left and click Create role.

    Create IAM task execution role for ECS

  2. In the Trusted entity type section, select AWS service.

    Networking Only cluster template

  3. In the Use case menu, choose Elastic Container Service Task and click Next.

    create a new VPC for this cluster

  4. Search and select AmazonECSTaskExecutionRolePolicy.

    Add permissions

  5. Select a name for the role and optionally assign tags as per your requirement. Review your settings and create the role.

    Select a name for the role and optionally assign tags

    Add Permission

  6. Verify that the role has been created.

    Verify that the role

Note - Click here to read more on How to manage AWS IAM user, Roles and Policies with Terraform



4. Creating an ECS Cluster

In the previous article, we had explained how we can push docker images to an ECR repository. We’ll now proceed to deploy a container on Fargate, from the nginx container image present in my ECR repository.

  1. To create ECS Cluster goto your AWS management console, search for Elastic Container Service, select Clusters and click Create Cluster.

    Create IAM task execution role for ECS

  2. Select the “Networking Only” cluster template.

    Networking Only cluster template

  3. You can create a new VPC for this cluster, or you can use an existing one. Set a name for your cluster and proceed with the creation of the cluster.

    create a new VPC for this cluster

  4. In the main Clusters dashboard, you’ll be able to see the status of your cluster.

    Add permissions



5. Creating Task definitions

We’ll now create a task definition to specify the requirements of our container. Create a new task definition and set the launch type to Fargate.

  1. Select “Task Definitions” from the navigation menu on the left side of the cluster dashboard.
    Create task definitions

ecsTaskExecutionRole IAM role

Fargate launch type

  1. Assign the “ecsTaskExecutionRole” IAM role to this task, which was created earlier. This is required to use the private registry authentication feature. This role will allow the container agent to pull the container images from the registry.

  2. The network mode can be set to default or “awsvpc”. The default is equivalent to docker’s bridge networking mode on Linux. We’ll use the “awsvpc” network mode. In this mode, ECS creates and manages a separate Elastic Network Interface for each task. This ensures that each task receives its own private IP address within the VPC.

Configure task and container definitions

  1. Next option is the task size. This should be defined based on the compute and memory requirements of the containerized applications

Task size

  1. Click “Add container” and specify the container image to be used for this deployment. You can specify the path of any external registry which is accessible from the AWS network.

Add a Container

Here is the launch status

Launch Status



6. Running a Task

  1. Now that we’ve created a task definition, we’ll try to run our task. Go to task definitions and click “Run task” under the actions menu.

Run task

  1. Define the VPC, subnets, security groups. You can enable the “Auto-assign public IP” if you want your service to be on a public network.

Auto-assign public IP

VPC and Security Groups

VPC and security group

  1. Check the status of your task. It might stay in the “Provisioning” state for a few seconds.

task status

  1. If you enabled the assignment of public IP, note down the public IP.

Fargate running status

  1. Try accessing the public IP from the browser and you’ll see the default nginx web page.

Check the status of Nginx



7. Creating a new task revision

If you want to modify anything in your task, you can create a new revision. You can change the CPU and memory assignments, or the container image itself.

  1. To create a new revision, in your task definition dashboard, click “Create new revision” and specify the changes that you want to make.

Create new revision for task

New revision of task

8. Creating an ECS Service

To create an ECS service, select “Create Service” under the Actions menu on the task definition page. Specify the number of tasks that you want to run. You can also choose to put the service behind a load balancer and set an autoscaling group.

Create ECS service

Configure service

Latest prod cluster

Load balancing

Set auto scaling

Set auto scaling

Create service

Verify that there will be three tasks running now, which is the same as the “Desired number of tasks” as defined in our service configuration.

9. Conclusion

With ECS Fargate, end users and developers don’t need to worry about managing the infrastructure for their containerized applications. This removes complexities and speeds up the deployment process.