Deploy Microservices on AWS ECS with Fargate (Serverless)
Tags: aws, devops, docker, microservices
Published: 2021-09-12 12:16:04

---
series: Containers on AWS
---
Amazon ECS (Amazon Elastic Container Service) is a highly scalable and fully managed container management service that makes it a breeze to run, stop, and manage containers on a cluster. You can use it to deploy your containerized applications or your microservices.

***
Before starting, here's a summary of key concepts in AWS ECS:

#### Cluster
A logical grouping of your services or tasks

#### Task Definition
Specification about how your container(s) should be run on AWS ECS

#### Task
An instance of the `Task Definition`

#### Service
A task manager

***
### Section 1: Create an AWS ECS Cluster
***
From the AWS console, navigate to AWS ECS
![AWS ECR from AWS Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6mrgbu1n94fa7okhj5e3.png)

Click 'Get started'
![AWS ECS - Getting Started](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/429wx6i3kte25uwnleiu.png)

Click 'Create cluster'
![AWS ECS - Create Cluster](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ntawo5kbp76q39bmn05f.png)

Select 'Network only' as cluster template, click `Next step`
![AWS Cluster creation template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0swzeojn7tm6jkcz2iq5.png)

Configure your cluster and click 'Create'

*Cluster name*
Give any meaningful name to your cluster

*Create VPC (optional)*
Enable to launch your cluster in a VPC (A VPC is an isolated portion of the AWS Cloud)

*Tags (optional)*
You can use them to help you organize/group your AWS resources

*CloudWatch Container Insights*
Check to enable container insights (failures, restarts, CPU utilization, memory usage, .etc) and view them inside [AWS Cloud Watch](https://aws.amazon.com/cloudwatch/)
![AWS ECS Cluster Configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/883uuaup1ssumtqs9p33.png)

Once the cluster has been created, click 'View Cluster'
![AWS ECS Cluster Creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/263r2rps63bor55v0anv.png)

***
### Section 2: Create an AWS ECS Task Definition
***
From the left sidebar (expand it by clicking the hamburger menu from the left top), click 'Task Definitions'
![AWS ECS Task Definition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipc96asf9t5fgij4yr97.png)

Click 'Create new Task Definition'
![Create new Task Definition in AWS ECS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jvzzrr5yfr6wlkq8jdud.png)

Select 'Fargate' and click 'Next step'
![Create Fargate type Task Definition in AWS ECS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ucstnlcpsoefwn9w1jao.png)

Give a meaningful name to your task definition, leave the `Task Role` field empty (or you can create a new role from [IAM](https://aws.amazon.com/iam/) and select it here), and leave the 'Network mode' to `awsvpc` (when launching as the Fargate type, only `awsvpc` can be selected)

![AWS ECS Task Definition Configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ljfnhnf5thog4xm8d1n.png)

Select 'Create new role' (or select any other existing role that you have created, and configure vCPU and Memory size for this task (this determines what charges you will be paying for this task)
![Task Definition Resources in AWS ECS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zv002utsrzqxnutbucz5.png)

Click 'Add container'
![Add container to AWS ECS Task Def](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6h4c1n6ul7q42j11c9n.png)

Give a name to your container, in the image field paste the image URI of the image (If you are not familiar with AWS ECR then [read this first](https://dev.to/mubbashir10/push-docker-images-to-aws-ecr-4l7e)), add port to which your container listens to (for example if your nodejs app is running on 3000, then you should enter 3000 in this field)

You can skip the `Advanced container configuration` but if you need to configure other aspects of your container (like health checks, volumes, networking, the environment variable, etc. then you can do that from this section). 

***
ℹ️ **Note: You can add multiple containers under a Task definition**
***

Once the container configuration is completed, click 'Add'

![Add container to AWS ECS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2i59ftn0qcorm559gf5t.png)

You can skip the integrations with [AWS Firelens](https://aws.amazon.com/about-aws/whats-new/2019/11/aws-launches-firelens-log-router-for-amazon-ecs-and-aws-fargate/) and AWS App Mesh unless you need it. If you need to add volumes that should be accessible to every container within a task, do so from the volumes section. The Tags are optional and let you organize your AWS resources.

Click 'Add'
![Configure option for AWS ECS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6m6wcsl84oiqjh44jtp.png)

Once the task definition has been created, it will show you a success message
![AWS ECS Clusters, Tasks, and Services](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwnvx0ry1kumfiavkxqj.png)

***
### Section 3: Create an AWS ECS Service
***

Now go back to your cluster and click 'Deploy' (from the services section)
![Create a service in AWS ECS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuxmwkn8zp2bwq2typ33.png)

Leave everything to default
![AWS ECS Clusters, Tasks, and Services](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmu6kg0077nb3s9h6ivt.png)

*Application Type:*
Service

*Specify revision manually*
Uncheck

*Family*
Select the Task Definition you created earlier

*Revision*
Latest

*Service Name*
Any meaningful name

*Desired Tasks*
Enter the number of Tasks (instances) you want to run 

![Service deployment configuration AWS ECS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nitif21p2lfff31zf62g.png)

`Load balancing` is optional but recommended. Configure a load balancer to distribute incoming traffic across the tasks running in your service.

- Click on 'Create a new load balancer'
- Give any meaningful name
- Enter in the port where you want AWS ELB (elastic load balancer) to listen on (this is not the container's port that you configured earlier in the task definition)
- Select HTTP/HTTPS as protocol (for HTTPS you will need to have a valid SSL certificate)
- Target group routes traffic from AWS ELB to tasks/instances; give any name and select the protocol as HTTP

![AWS ELB in AWS ECS Service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/58cu92c3e8gk9jxpxe72.png)

In the Networking section:
- Select the VPC in which your cluster and task definitions are (by default, it's always already selected)
- Select subnets (within VPC) where you want to deploy the task (I have selected all available subnets under my VPC)
- Select "Use an existing security group", and select a security group that allows inbound HTTP traffic - [learn how to create](https://dev.to/mubbashir10/control-inbound-outbound-traffic-using-aws-security-groups-4h69)
- Every task can be associated with a Public IP address, disable it if you don't need a public IP

![Networking in AWS ECS Service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9vrntw5h5aaxbvd76j6.png)

Add tags if you want to and then click `Deploy`
![Deploy services on AWS ECS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1a0ssovx3vdh8jmz54e8.png)

Once the service has deployed, it will change the status to "Active"
![AWS ECS Service Deployed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1tmuetpr8pj4vozp0idq.png)

***
### Section 4: Access the AWS ECS Service
***
To access the service you can go to `AWS EC2-> Load Balancers->Your Load Balancer Name` and then copy the A-Record
![AWS ECS Clusters, Tasks, and Services](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8vzfhpq3rgpaw6by9k6.png)

To access an individual task (provided you enabled the public IP earlier), go to `AWS ECS->Services->Your Service->Logs->Your Task->Network->Public IP` ![AWS ECS Clusters, Tasks, and Services](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s832nn2yz316h63hhwtn.png)

---
Let's connect: 

Linkedin: [https://www.linkedin.com/in/mubbashir10/](https://www.linkedin.com/in/mubbashir10/) 

Twitter: [https://twitter.com/mubbashir100](https://twitter.com/mubbashir100)