Amazon Elastic Container Service (ECS)
Overview
AWS Elastic Container Service (ECS) is a fully managed container deployment service by Amazon. It enables you to deploy your containerized applications easily without having to configure an environment for the code to run it, with features like autoscaling and autorecovery. ECS comes with two launch types: AWS EC2 and AWS Fargate.
What is AWS ECS?
Let's imagine you have just finished building an awesome application on your laptop and now you deploy it globally so anyone can use your application. Maybe you can use AWS EC2 Instances to deploy your application? But then you will need to install the required libraries and then deploy your code. Instead what if you could bundle your code into a package and let AWS handle the deployment, along with automatic scaling and monitoring?
Amazon has designed AWS Elastic Container Service (ECS) for precisely this purpose. AWS ECS provides a convenient container management service that is highly scalable and automatable. It enables you to run images on the fly and fully utilize the benefits of containerization.
Let's first understand the differences between an "image" versus a "container" and then jump into the depths of AWS ECS.
Important Terminologies
Before delving into AWS ECS and its inner workings, we need to understand the following key terminologies:
- Container: A container "contains" all the required dependencies, libraries, and code to run an application. Containers are like mini virtual machines which are independent of the underlying hardware's operating system. This enables containers to be portable and scalable.
- Image: If the container enables you to run applications, images enable you to "save" the required dependencies, libraries, and code. Images are like snapshots of the mini virtual machines and can be shared easily.
- Docker: Docker is one of the ways of running containers. Docker can be used to run an image as a container on your laptop or in a production environment. This enables you to use the same image across different environments without changing the application code.
Components of AWS ECS
Other than containers and images, which we learned about in the previous section, AWS ECS is made up of various components:
- Task Definitions: A task definition is a JSON file that contains the description of all the containers that form your application. Configurations like which ports to open for your application, which containers are dependent on each other, and what data volumes to use can be mentioned in the task definition. A task definition can contain up to a maximum of ten container descriptions.
- Tasks: When you run a task definition in AWS ECS, a task is created. You can define the number of tasks to run in a cluster, as well as if the task should be standalone or part of a service.
- Services: An AWS ECS service can be used to manage tasks in a cluster, and maintain a defined number of running tasks. When you use a service to run a task, you do not need to worry about failure recovery.
- Clusters: An AWS ECS cluster represents a group of tasks or services. Your application can use clusters to run independent tasks in isolation, which lets AWS ECS scale each cluster separately as per demand.
- Container Instance: A container instance is a single EC2 instance in the AWS ECS cluster when you choose to use the "EC2 launch type".
- Container Agent: A container agent is a service that runs on each container instance in an AWS ECS cluster. The agent is responsible for sending data about the currently running task and resource utilization.
- AWS ECR: AWS Elastic Container Registry (ECR) is an online image repository where users can store, share, and deploy container images. It is an important component of AWS ECS as AWS ECS clusters pull images from AWS ECR to run tasks.
Features of AWS ECS
AWS ECS comes with a whole host of features that makes deploying container-based applications easy and efficient.
- Strong Developer Support: AWS ECS has strong developer support in the form of Docker compatibility and AWS Copilot. AWS ECS enables you to run and manage Docker containers without any configuration changes. It also lets you use multiple images in one container with the help of the Docker Compose tool. Amazon also provides its own tool, AWS Copilot, to easily build, release and operate containerized applications. It automates the deployment process and enables health monitoring of your services.
- Intelligent Autoscaling and Autorecovery: AWS ECS has intelligent autoscaling and autorecovery built-in. AWS ECS can scale the number of tasks as per the application's demands and automatically recover unhealthy containers.
- Task Scheduling: In AWS ECS, you can design tasks to run on a specific schedule and shut down automatically after the processing has been completed. The task scheduling feature of AWS ECS is both powerful and cost-effective.
- Security and Isolation By Design: AWS ECS promotes security by design. It integrates with AWS IAM and you can assign granular permissions for each of your containers. You can run applications in clusters that are completely isolated from each other.
- In-Built Monitoring and Logging: AWS ECS integrates with AWS CloudWatch and AWS CloudTrail to enable monitoring and logging without any hassle. You can monitor your container's resource utilization and analyze logs without leaving the AWS Console.
- Enabling Hybrid Deployments: Using AWS Outposts and AWS ECS together, you can run containerized applications that depend on low latencies with on-premises hardware. AWS Outposts is a fully managed service that brings AWS infrastructure to your environment.
AWS ECS Use Cases
AWS ECS can be used in a variety of scenarios, a few of which are listed below:
- Microservices: AWS ECS can be used to host an application following a microservice architecture, with each service defined as a separate task definition. AWS ECS can scale each service independently and handle the automated deployment via a CICD system.
- Websites and APIs: AWS ECS can host websites or backend APIs. An AWS Application Load Balancer (ALB) can be used to drive internet traffic to AWS ECS clusters.
- Machine Learning: AWS ECS clusters can be used in Machine Learning applications, particularly for training models and using trained models for inference.
- Batch Processing: AWS ECS is a powerful option for applications that require batch processing. With its task scheduling features, processes can be scheduled in advance and resources will be automatically managed by AWS ECS.
- Cloud Migration: Since AWS ECS supports a variety of containers, including both Linux-based and Windows-based images, it can be used when a company is moving its infrastructure to the cloud without many changes in the application code.
AWS ECS Launch Types
There are two launch types available in AWS ECS:
EC2 Launch Type
In this launch type, you configure and deploy EC2 instances in your AWS ECS cluster to run your containers.
This launch type can be used for the following workloads:
- Workloads that require consistently high CPU core and memory usage
- Large workloads that need to be cost-effective
- Applications that require access to persistent storage
- When you need to directly manage your infrastructure for compliance or other reasons
Fargate Launch Type
In this launch type, containers run without any user management. AWS manages the infrastructure and your containers run in a serverless pay-as-you-go environment.
This launch type can be used for the following workloads:
- Small workloads that have occasional bursts of demands
- Batch processing workloads
- Serverless applications
- Applications where user management is not required and can be handled automatically
Getting Started with AWS ECS
There are many ways to deploy containers on AWS ECS. Let's take a look at the options:
- Using the AWS Console: The quickest to get started with AWS ECS is by using the AWS Console. Upload your image to AWS ECR and use the AWS ECS dashboard to create a task and run your containers.
- Using AWS Copilot: AWS Copilot provides a comprehensive way to build, test, and deploy containers to AWS ECS. This is a CLI tool provided by AWS and is available on Windows, Mac OS, and Linux-based OSes.
- Using AWS CDK: If you want to programmatically control your AWS ECS deployment, the AWS Cloud Development Kit (CDK) is an excellent choice. It provides helpful templates and tooling to easily build task definitions and create clusters.
For detailed instructions, please refer to the official AWS Documentation, linked here.
List of Available Commands
AWS provides a few commands that can be run using the AWS CLI to interact with AWS ECS. Here are a few key commands:
- create-cluster: Creates a new AWS ECS Cluster.
- describe-clusters: Describes one or more of ECS Clusters.
- describe-services: Describes Services running in a specified cluster.
- list-clusters: Gives a list of existing clusters.
- run-task: Starts a new task from a task definition.
- delete-cluster: Deletes an entire specified cluster.
Benefits of AWS ECS
Using AWS ECS has a lot of benefits compared to other container-based deployment solutions:
- Performance at Scale: AWS ECS enables your containerized applications to scale dynamically and automatically, giving your application performance at scale with minimal configurations or application code changes.
- Integration with AWS Services: AWS ECS is tightly integrated with other AWS Services. Some examples include AWS CloudWatch for monitoring, AWS CloudTrail for logging, AWS ALB for load balancing, and AWS ECR as an image repository.
- Cost Efficient: AWS ECS is a cost effective way of running your containerized applications. AWS ECS automatically manages the hardware and further helps you save costs with features like task scheduling for short-lived tasks and automatic recovery of unhealthy containers.
- Improved Security: AWS ECS allows you to run applications in isolated containers and provide granular permissions to containers, which enhances the security of your application as well as your infrastructure.
AWS ECS Pricing
In the case of either the AWS EC2 Launch Type or AWS Fargate Launch Type, you only pay for the resources you use - there is no upfront cost for using AWS ECS.
For the AWS EC2 Launch Type: The pricing model is similar to paying for AWS EC2 Instances. You can choose from multiple ways to pay - from On Demand, Savings Plan, Reserved Instances, or Spot Instances.
For the AWS Fargate Launch Type: The pricing models calculate costs based on the vCPU and memory on an hourly basis. The hourly cost depends on the Operating System and CPU Architecture chosen. You also have to pay additionally for any Storage costs.
For example, for a Linux x86 based system, the hourly costs are - $0.04048 per vCPU per hour and $0.004445 per GB per hour (These rates are for us-west-2; the rates may vary per region).
Amazon Elastic Container Service (ECS) vs. Amazon Elastic Kubernetes Service (EKS)
Amazon Elastic Kubernetes Service (EKS) is another managed container deployment offering by AWS which uses Kubernetes for orchestration. Here are the differences between AWS ECS and AWS EKS:
AWS ECS | AWS EKS | |
---|---|---|
Complexity | AWS ECS is relatively much easier to setup and manage than AWS EKS. Consequently, developing applications is easier on AWS ECS. | AWS EKS requires a lot of configurations to initially setup and then manage, making it comparatively more complex to develop than AWS ECS. |
Security | You can provide granular permissions using AWS IAM. | For permissions, AWS EKS requires additional plugins/configurations to use AWS IAM. |
Pricing | You only pay for the resources you use. | You need to pay for both - the resources you use and a separate charge for hosting the AWS EKS cluster. |
Portability | You cannot move your task definitions from AWS ECS to other container-based deployment solutions as they are proprietary. | Since AWS EKS is based on Kubernetes, you can move your application to other Kubernetes-based deployment solutions. |
Conclusion
- AWS ECS is a fully managed container deployment service offered by AWS.
- AWS ECS is made up of different components - task definitions, tasks, services, clusters, container instances, container agents, and AWS ECR.
- AWS ECS has the following features - strong developer support, intelligent autoscaling and autorecovery, task scheduling, in-built monitoring and logging, and strong security.
- Some common use cases of AWS ECS are - microservices, hosting websites and APIs, machine learning applications, batch processing, and cloud migrations.
- There are two launch types available - AWS EC2 (based on EC2 instances) and AWS Fargate (serverless).
- There are many ways of getting started with AWS ECS - using the AWS Console, AWS Copilot, or AWS CDK.
- Using AWS ECS has a lot of benefits - performance at scale, improved security, cost efficiency, and integration with AWS Services.
- AWS ECS does not have any upfront charges and you pay only for the resources you use.
- AWS ECS differs in many ways compared to AWS EKS - AWS EKS is more complicated to use but offers deployment portability as it is built on top of Kubernetes.