DEV Community

Mubbashir Mustafa
Mubbashir Mustafa

Posted on • Updated on

Deploy Microservices on AWS ECS (Fargate) - Serverless

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 we continue, 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

Click 'Get started'
AWS ECS - Getting Started

Click 'Create cluster'
AWS ECS - Create Cluster

Select 'Network only' as cluster template, click Next step
AWS Cluster creation template

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
AWS ECS Cluster Configuration

Once the cluster has been created, click 'View Cluster'
AWS ECS Cluster Creation


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

Click 'Create new Task Definition'
Create new Task Definition in AWS ECS

Select 'Fargate' and click 'Next step'
Create Fargate type Task Definition in AWS ECS

Give a meaningful name to your task definition, leave the Task Role field empty (or you can create a new role from 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

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

Click 'Add container'
Add container to AWS ECS Task Def

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), 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

You can skip the integrations with AWS Firelens 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

Once the task definition has been created, it will show you a success message
AWS ECS Clusters, Tasks, and Services


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

Leave everything to default
AWS ECS Clusters, Tasks, and Services

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

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

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

Add tags if you want to and then click Deploy
Deploy services on AWS ECS

Once the service has deployed, it will change the status to "Active"
AWS ECS Service Deployed


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

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


Let's connect:

Linkedin: https://www.linkedin.com/in/mubbashir10/

Twitter: https://twitter.com/mubbashir100

Discussion (0)