Setup containerized Application in AWS ECS - Part 2/3
Tags: aws, cicd, terraform, ecs
Published: 2023-12-07 08:57:50

In the previous [Part 1/3](https://dev.to/camillehe1992/setup-containerized-application-in-aws-ecs-part-13-2emk), I introduced how to setup a MYSQL database in AWS RDS. Now, we will dive into next topic - Setup ECS Cluster in AWS ECS. 

Amazon Elastic Container Service (Amazon ECS) is a fully managed container orchestration service that helps you easily deploy, manage, and scale containerized applications. The article will focus on the ECS services and how them work together to manage your containerized application in the Cloud.

## :anchor: Amazon ECS Concepts 

Here is the architecture diagram of the entire project.
![arch-overall](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kv5xrbybwx6yn5gwcyp1.png)

The AWS resources in green box are created in this part, which include:

### ECS Cluster & ECS Capacity

An Amazon ECS cluster is a logical grouping of tasks or services. There are two capacity options: EC2 and Fargate. In this project, I setup ECS Cluster on EC2 type. You can find a article that talks about the differences between ECS and Fargate at the bottom of article. 

### Capacity Provider

A capacity provider defines the cluster capacity that Amazon ECS scales up and down of the infrastructure you specify. For Amazon ECS on Amazon EC2, a capacity provider consists of a capacity provider name, an Auto Scaling group.

### Amazon EC2 Auto Scaling

As we use Amazon EC2 instances for ECS capacity, I use Auto Scaling groups to manage the Amazon EC2 instances registered to ECS cluster. Auto Scaling ensure that we have the correct number of Amazon EC2 instances available to handle the load for our application.

### Launch Template

A launch template contains the configuration information to launch an instance in EC2 Auto Scaling, including the ID of the Amazon Machine Image (AMI) and provides full functionality for Amazon EC2 Auto Scaling.

### SNS Topic & Lambda Function

On the right top of the diagram, we setup a SNS topic and Lambda function to automate container instance draining in Amazon ECS. It's not mandatory for the whole project, but good to have in a real world project. I won't dive into the details here, but go through the [blog](https://aws.amazon.com/blogs/compute/how-to-automate-container-instance-draining-in-amazon-ecs/) if you're interested the topic. 

## Terraform Project

Same as before, the AWS resources are defined using Terraform. Here is the GitHub source code https://github.com/camillehe1992/aws-terraform-examples/tree/main/ecs-cluster-on-ec2

Walk through the README documentation in above GitHub repository, and setup local environment if you want to deploy these AWS resources from local machine, or you can use GitHub Actions workflows.

After done, validate the setup is successful from ECS Console. Your ECS Cluster must have 1 registered container instance as the below picture shows.
![registery-instances](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oppgldnytihi6hnfvnqe.png)

Next to [Setup containerized Application in AWS ECS - Part 3/3](https://dev.to/camillehe1992/setup-containerized-application-in-aws-ecs-part-33-3fee)

## :books: References

- https://cloudonaut.io/ecs-vs-fargate-whats-the-difference/
- https://docs.aws.amazon.com/AmazonECS/latest/developerguide/asg-capacity-providers.html
- https://docs.aws.amazon.com/autoscaling/ec2/userguide/create-launch-template.html
- https://aws.amazon.com/blogs/compute/how-to-automate-container-instance-draining-in-amazon-ecs/

Always appreciate for your ideas and comments. Thanks for reading! :smile: