Manage your containers on AWS ECS
Tags: aws, docker, tutorial, vue
Published: 2021-03-06 19:26:10

Hey Dev's

How have the tutorials for docker been coming along?. So far we have simplified how to learn Docker using two files, we have learned how to push images to AWS ECR or Docker Hub. 

For production, we will need to manage our containers. This is where AWS ECS comes along. 

#Brief explanation

Amazon Elastic Container Service (ECS) is a highly scalable, high-performance container management service that supports Docker containers and allows you to easily run applications on a managed cluster of Amazon EC2 instances. Amazon ECS eliminates the need for you to install, operate, and scale your own cluster management infrastructure.

#### Benefits
- Application first with Capacity Providers
- Performance at scale
- Secure
- Reliable
- Optimized for cost

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7m7m0xkv0o2qhwz7rsw6.png)

#### Use cases
- Hybrid Development
- Batch processing
- Machine Learning
- Web Applications

#### Pricing
There are two different charge models for Amazon Elastic Container Service (ECS).

- Fargate launch Type Model
With Fargate, you pay for the amount of vCPU and memory resources that your containerized application requests. Read more about the pricing of Fargate [https://aws.amazon.com/fargate/pricing/](https://aws.amazon.com/fargate/pricing/)

- EC2 launch Type Model
There is no additional charge for EC2 launch type. You pay for AWS resources (e.g. EC2 instances or EBS volumes) you create to store and run your application. Read more about the pricing of EC2 [https://aws.amazon.com/ec2/pricing/](https://aws.amazon.com/ec2/pricing/)


Now that we have a brief explanation of what AWS ECS is let us get practical and learn how to deploy our containers on AWS ECS.

With ECS there are three components that will simplify your understanding in working with AWS ECS. 

- Task.

A task definition is like a blueprint for your application. Each time that you launch a task in Amazon ECS, you specify a task definition. The service then knows which Docker image to use for containers, how many containers to use in the task, and the resource allocation for each container.

- Cluster

An Amazon ECS cluster is a logical grouping of tasks, services, and container instances. When creating a cluster using the console, Amazon ECS creates an AWS CloudFormation stack that takes care of the Amazon EC2 instance creation, networking, and IAM configuration for you.

- Service

An Amazon ECS service enables you to run and maintain a specified number of instances of a task definition simultaneously in an Amazon ECS cluster. If any of your tasks should fail or stop for any reason, the Amazon ECS service scheduler launches another instance of your task definition to replace it in order to maintain the desired number of tasks in the service.

The steps for deploying your images to ECS will be you need to create a cluster, a Task definition with all the configuration, and a service.

In our previous tutorial, we learned how to push our images to AWS ECR and Docker Hub [https://dev.to/kevin_odongo35/aws-ecr-or-docker-hub-4m7o](https://dev.to/kevin_odongo35/aws-ecr-or-docker-hub-4m7o).

We have a default Vue application. We built an image and pushed it to AWS ECR. We will deploy this to the ECS cluster. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3uqbrday8b69lurldz1.png)

# STEPS

- Once we have our image in ECR then we can go ahead and deploy our cluster. Search for ECS and click get started. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/exrk663o1fbxgv6ff1h8.png)

- Get the image URI from AWS ECR and configure the custom section then click next.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlmpj9ydqxp6m957p1da.png)

- In the next section select Application Load Balance.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0habu9x02x7dd5qg1tw.png)


In AWS we will an Application Load Balancer to direct traffic to each container. With ALB we can define different target groups and assign them to different clusters. 

**Target group is used to route requests to one or more registered targets.**

Read more about Application Load Balance target groups [https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html).

Once done review and click create.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pn1zdtl2ep0dad6qyyrf.png)

Once deployed go to EC2 Dashboard and search for Load Balancers. Get the DNS Record. Your application should be running.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x96kh83u08kocfp4ahk9.png)

How to create a new version.

Assume you have changed your website and would like to deploy a new version. Build a new image with the new version and tag it then push it to AWS ECS. In below image you can see i have two versions in my ECR.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/up5q2at7lqiliqa2byox.png)

Use the image to create a new task definition and update the service.

# CONCLUSION

This is just a quick way to get started with AWS ECS. In a large production, you need to understand how to use Application Load Balance especially configuring target groups because that is the way traffic to your containers will be routed. Just as Nginx does.

I hope this will be helpful to someone who wants to get started with ECS. In my own opinion using ECS is easy to get you started with container orchestration.

Thank you