{{< partial "learn_x_header" >}}
## The Basics
Amazon ECS, the Elastic Container Service, is an Amazon alternative to [Docker Swarm], [Kubernetes], and [HashiCorp Nomad]. Amazon aims to make ECS as simple as possible to reduce the complexity of deploying container-based workloads. Under the hood, ECS runs docker on EC2 instances.
### Primary Use Cases
- Maximize EC2 compute resource usage
- Scalable deployment of container workloads
- Scalable batch processing
### Less Suitable Use Cases
- Simple web application or website hosting.
- Consider Amazon S3 or [AWS Amplify] instead.
_Amazon ECS contains these core layers._
{{< figure src="images/ecs-overview.png" alt="ECS Overview" >}}
Review [Amazon Elastic Container Service template snippets] for a big-picture view of CloudFormation [YAML] resources.
### When to Use ECS?
Use ECS when you need to run containerized workloads in the cloud. ECS is a versatile service that can meet a wide range of use cases.
## Dependencies
ECS components optionally depend on [EC2], Auto Scaling, Load Balancers, and CloudWatch to operate depending on your overall deployment strategy. Using Fargate simplifies deployment further by removing components from your software architecture.
I will now summarize each ECS component. Where applicable, a link to CloudFormation resource documentation is present.
## Cluster
A Cluster is a logical grouping of Tasks or Services running on an [EC2] instance, otherwise known as a Container Instance.
**Example:** fancy-web-application-cluster
The primary responsibility of an [Amazon ECS Cluster] is managing infrastructure for your Tasks.
[AWS::ECS::Cluster]
Describe a cluster with this command.
```sh
aws ecs describe-clusters --cluster fancy-web-application-cluster
```
## Container Instance
An Amazon ECS container instance is an [EC2] instance running an Amazon ECS container agent registered into an Amazon ECS cluster.
The primary responsibility of an [Amazon ECS container instance] is computing resources for your ECS cluster.
Describe container instances with this command.
```sh
aws ecs describe-container-instances --cluster fancy-web-application-cluster
```
## Service
Manages the desired state of your tasks. It defines the number of Tasks to run, autoscaling, and load-balancing strategies. It can optionally work with an Amazon load balancer.
**Example:** fancy-web-application-service
The primary responsibility of an [Amazon ECS Service] is keeping your Cluster healthy.
[AWS::ECS::Service]
Describe an ECS Service with this command.
```sh
aws ecs describe-services --services fancy-web-application-service
```
## Container Agent
Allows container instances to connect to your Cluster.
The [Amazon ECS container agent] is primarily responsible for managing containers on Amazon ECS.
The ECS container agent runs on each Container instance within a cluster and sends telemetry data about that instance's tasks and resource utilization to the ECS service.
The Amazon ECS container agent ships with ECS-optimized AMIs, but you can also install it on any Amazon EC2 instance that supports the Amazon ECS specification.
## Task Definition
_I'm using [an example webserver task definition] provided by AWS for reference. [The Task Definition parameters documentation] provides a more comprehensive overview._
A _Task Definition_ is a core ECS concept. Describe [a Task Definition] using a JSON file.
A _Task Definition_ is a collection of container configurations. The _Task Definition_ allows you to specify which Docker image to use, which ports to expose, how much CPU and memory to allocate, how to collect logs, and define environment variables. A task may need one container, while others may require two or more containers.
[AWS::ECS::TaskDefinition]
A _Task Definition_ includes these properties.
#### Family
A Task Definition family is a name for a group of versioned containers.
**Examples**: fancy-web-api, fancy-web-server
[More...][family more...]
#### Launch types
A Task Definition's _Launch types_ tell Amazon which environment to deploy containers.
Possible values are EC2, Fargate, and External.
**Example:** EC2
[More...][launch types more...]
#### CPU
A Task Definition's hard limit of CPU units allocated to a task.
A value of 1024 represents one vCPU.
**Example**: 1024
[More...][task definition more...]
#### Memory
A hard limit of memory (in MiB) allocated to the task.
**Example:** 1024 ( 1 GB )
[More...][memory more...]
#### Container Definition
An array of up to ten container definitions. A _Container Definition_ is the core component of a Task Definition.
The container definition's CPU and memory allocations are separate from the Task Definition's properties.
[More...][container definition more...]
##### Name
A name for a container.
The Name property has a maximum length of 255 characters. Use the name of the container to form links between containers.
**Example:** Nginx
[More...][container definition name more...]
##### Image
Defines the container that starts with the task definition using a docker repository and tag name.
Images in the Docker Hub registry are available by default. Use the **repository-url/image:tag or repository-url/image@digest** format for container registries outside Docker Hub.
**Example:** debian:latest, aws_account_id.dkr.ecr.region.amazonaws.com/my-web-app:latest
[More...][container image more...]
##### Memory
The amount (in MiB) of memory allocated to the container. Use the memoryReservation property to set the minimum required memory for your container. Leave the memory property blank unless you need your container to terminate when memory consumption exceeds the defined limit.
**Example:** _memory:_ 1024, _memoryReservation:_ 128
[More...][container memory more...]
##### Port Mappings
Optional port mappings allow containers to access ports on the host instance to send or receive traffic.
**Example:** 8080
[More...][container port mappings more...]
Describe a task definition with [this AWS CLI command.]
```sh
aws ecs describe-task-definition
```
## LaunchTypes
I [introduced Task Definition LaunchTypes] previously. A choice of LaunchType fundamentally changes your architecture. Fargate simplifies your architecture and, depending on requirements, can be cheaper than an ECS LaunchType architecture. Fargate controls the underlying infrastructure, while ECS LaunchType requires you to manage the underlying infrastructure.
Per Amazon's [comparison of Fargate VS. EC2 cost optimization].
_Fargate has an 87% saving over an EC2 equivalent when the EC2 memory and vCPU reservation rate are at 6.25% and 12.5%, respectively._
{{< figure src="images/costopchart.png" alt="On-Demand Price Difference" align="center" >}}
## Best Practices
- [Use each task definition family for only one business purpose]
- [Match each application version with a task definition revision within a task definition family]
- [Use different IAM roles for each task definition family]
- [Use awsvpc network mode and give each service its security group]
# Learn ECS - Beyond The Basics
- **Books**
- [Amazon Elastic Container Service: Developer Guide]
- **Videos**
- [Introduction to Amazon Elastic Container Service]
- [Elastic Container Service (ECS)]
## Related Content
- **Amazon**
- [Amazon Elastic Container Service (Amazon ECS)]
- [What is Amazon Elastic Container Service?]
- [Building Blocks of Amazon ECS]
- [Best Practices Guide]
- [Common use cases in Amazon ECS]
- [Deploy applications on Amazon ECS using Docker Compose]
- [Amazon ECS container agent configuration] — ECS Container Instance Options
- [Amazon Elastic Container Service identity-based policy examples]
- [Introducing AWS Copilot | Containers - Amazon AWS]
- [Setting up with Amazon ECS] — _Manually create the resources required for an ECS Cluster_
- [Tutorial: Using cluster auto scaling with the AWS Management Console]
- **Beginner**
- [A beginner’s guide to Amazon’s Elastic Container Service]
- [Gentle Introduction to How AWS ECS Works with Example Tutorial] — _Though I despise Medium_
- [A beginner’s guide to Amazon’s Elastic Container Service]
- **Code**
- [ECS + Terraform]
- [terraform-aws-modules]/[terraform-aws-ecs] — _Git Repository that includes an example ECS cluster in Terraform._
- [AWS]/[amazon-ecs-agent] — _Deployable ECS agent installed on ECS enabled AMIs by default_
- **Tools**
- [Installing or updating the latest version of the AWS CLI]
- TL;DR; if on mac: **brew install amazon-ecs-cli**
- **Troubleshooting**
- [How do you delete an AWS ECS Task Definition?]
- [Invalid CPU or memory value specified]
- [Amazon ECS container agent introspection]
- **Miscellaneous**
- [We switched to Amazon ECS and you won’t believe what happened next]
- [Scaling Docker on AWS: Estimating AWS Costs - Introduction]
- [The Seven Biggest Challenges of Deployment to ECS]
- [Deploy applications on Amazon ECS using Docker Compose] — _An alternate simplified way to deploy containers_
{{< partial "category_footer" >}}
<!--- Begin References --->
[docker swarm]: https://docs.docker.com/engine/swarm/
[kubernetes]: https://kubernetes.io/docs/concepts/overview/
[hashicorp nomad]: https://developer.hashicorp.com/nomad
[aws amplify]: /blog/2020/09/11/learn-aws-amplify/
[amazon elastic container service template snippets]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-ecs.html
[yaml]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-ecs.html#:~:text=Value%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Ref%22%3A%20%22taskdefinition%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D-,YAML,-AWSTemplateFormatVersion%3A%202010%2D09
[ec2]: /blog/2020/07/09/learn-ec2/
[amazon ecs cluster]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/clusters.html
[aws::ecs::cluster]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-cluster.html
[amazon ecs container instance]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ECS_instances.html
[amazon ecs service]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs_services.html
[aws::ecs::service]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-service.html
[amazon ecs container agent]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ECS_agent.html
[an example webserver task definition]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/example_task_definitions.html#example_task_definition-webserver
[the task definition parameters documentation]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html
[a task definition]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definitions.html
[aws::ecs::taskdefinition]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-taskdefinition.html
[family more...]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#:~:text=task%20definition%20parameter.-,Family,-family
[launch types more...]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/launch_types.html
[task definition more...]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#:~:text=a%20task%20definition%3A-,cpu,-Type%3A%20string
[memory more...]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#:~:text=Linux%2C%20Windows-,memory,-Type%3A%20string
[container definition more...]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#:~:text=Linux%2C%20Windows-,Container%20definitions,-When%20you%20register
[container definition name more...]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#container_definition_name
[container image more...]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#container_definition_image
[container memory more...]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#:~:text=Memory-,memory,-Type%3A%20integer
[container port mappings more...]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#:~:text=Port%20mappings-,portMappings,-Type%3A%20object%20array
[this aws cli command.]: https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ecs/describe-task-definition.html
[introduced task definition launchtypes]: #launch-types
[comparison of fargate vs. ec2 cost optimization]: https://aws.amazon.com/blogs/containers/theoretical-cost-optimization-by-amazon-ecs-launch-type-fargate-vs-ec2/
[use each task definition family for only one business purpose]: https://docs.aws.amazon.com/AmazonECS/latest/bestpracticesguide/application.html#:~:text=Use%20each%20task%20definition%20family%20for%20only%20one%20business%20purpose
[match each application version with a task definition revision within a task definition family]: https://docs.aws.amazon.com/AmazonECS/latest/bestpracticesguide/application.html#:~:text=Match%20each%20application%20version%20with%20a%20task%20definition%20revision%20within%20a%20task%20definition%20family
[use different iam roles for each task definition family]: https://docs.aws.amazon.com/AmazonECS/latest/bestpracticesguide/application.html#:~:text=Use%20different%20IAM%20roles%20for%20each%20task%20definition%20family
[use awsvpc network mode and give each service its security group]: https://docs.aws.amazon.com/AmazonECS/latest/bestpracticesguide/application.html#:~:text=Use%20awsvpc%20network%20mode%20and%20give%20each%20service%20its%20own%20security%20group
[amazon elastic container service: developer guide]: https://www.amazon.com/Amazon-Elastic-Container-Service-Developer/dp/9888408003
[introduction to amazon elastic container service]: https://app.pluralsight.com/library/courses/introduction-amazon-elastic-container-service/table-of-contents
[elastic container service (ecs)]: https://www.linkedin.com/learning/search?keywords=amazon%20elastic%20container%20service&u=2130809
[amazon elastic container service (amazon ecs)]: https://aws.amazon.com/ecs
[what is amazon elastic container service?]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/Welcome.html
[building blocks of amazon ecs]: https://aws.amazon.com/blogs/compute/building-blocks-of-amazon-ecs/
[best practices guide]: https://docs.aws.amazon.com/AmazonECS/latest/bestpracticesguide/intro.html
[common use cases in amazon ecs]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/common_use_cases.html
[deploy applications on amazon ecs using docker compose]: https://aws.amazon.com/blogs/containers/deploy-applications-on-amazon-ecs-using-docker-compose/
[amazon ecs container agent configuration]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html
[amazon elastic container service identity-based policy examples]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/security_iam_id-based-policy-examples.html
[introducing aws copilot | containers - amazon aws]: https://aws.amazon.com/blogs/containers/introducing-aws-copilot/
[setting up with amazon ecs]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/get-set-up-for-amazon-ecs.html
[tutorial: using cluster auto scaling with the aws management console]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/tutorial-cluster-auto-scaling-console.html#console-tutorial-capacity-provider
[a beginner’s guide to amazon’s elastic container service]: https://www.freecodecamp.org/news/amazon-ecs-terms-and-architecture-807d8c4960fd/
[gentle introduction to how aws ecs works with example tutorial]: https://medium.com/boltops/gentle-introduction-to-how-aws-ecs-works-with-example-tutorial-cea3d27ce63d
[a beginner’s guide to amazon’s elastic container service]: https://www.freecodecamp.org/news/amazon-ecs-terms-and-architecture-807d8c4960fd/
[ecs + terraform]: https://github.com/alex/ecs-terraform
[terraform-aws-modules]: https://github.com/terraform-aws-modules
[terraform-aws-ecs]: https://github.com/terraform-aws-modules/terraform-aws-ecs
[aws]: https://github.com/aws
[amazon-ecs-agent]: https://github.com/aws/amazon-ecs-agent
[installing or updating the latest version of the aws cli]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ECS_CLI_installation.html
[how do you delete an aws ecs task definition?]: https://stackoverflow.com/questions/35045264/how-do-you-delete-an-aws-ecs-task-definition
[invalid cpu or memory value specified]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html
[amazon ecs container agent introspection]: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-introspection.html
[we switched to amazon ecs and you won’t believe what happened next]: https://blog.mapbox.com/we-switched-to-amazon-ecs-and-you-wont-believe-what-happened-next-6cadbbf7282c
[scaling docker on aws: estimating aws costs - introduction]: https://youtu.be/AVpSZWoRas8
[the seven biggest challenges of deployment to ecs]: https://convox.com/blog/ecs-challenges/
[deploy applications on amazon ecs using docker compose]: https://aws.amazon.com/blogs/containers/deploy-applications-on-amazon-ecs-using-docker-compose/