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
- Getting started with AWS ECS and Fargate
- Prerequisites
- Create IAM task execution role for ECS
- Creating an ECS Cluster
- Creating Task definitions
- Running a Task
- Creating a new task revision
- Creating an ECS Service
- 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.
- ECS cluster
- Task definitions
- Tasks
- 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
Go to the IAM console, select Roles from the navigation menu on left and click Create role.

Create IAM task execution role for ECS In the Trusted entity type section, select AWS service.

Networking Only cluster template In the Use case menu, choose Elastic Container Service Task and click Next.

create a new VPC for this cluster Search and select AmazonECSTaskExecutionRolePolicy.

Add permissions 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 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.
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 Select the “Networking Only” cluster template.

Networking Only cluster template 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 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.
- Select “Task Definitions” from the navigation menu on the left side of the cluster dashboard.

Create task definitions


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

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

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

Here is the launch status

6. Running a Task
- 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.

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

VPC and Security Groups

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

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

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

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.
- To create a new revision, in your task definition dashboard, click “Create new revision” and specify the changes that you want to make.


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.







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.