Amazon Elastic Container Service (ECS) is a fully managed container orchestration service that enables users to easily run, manage and scale containers on AWS. With ECS, you can deploy containers either on a cluster of Amazon EC2 instances or on AWS Fargate, a serverless computing engine for containers. In this article, we’ll look at how these two launch types compare and explore how to start using them.
ECS is highly scalable and powerful for orchestrating containers that are run on Docker. You can use it to deploy containers from your local environment via Docker and provision access to those containers from anywhere, resulting in zero downtime and no complicated configurations. You can also scale numerous containers with just a single click, then collate logs about the container instance to Amazon CloudWatch.
ECS provides the underlying infrastructure to create task definitions for your tasks and services; however, you still have to select a launch type that will handle running, stopping, and deploying your tasks and services.
In this article
To further understand ECS, it’s essential to understand the components involved in deploying a service with ECS. These include:
Let’s look at each briefly to gain a better understanding of how Amazon ECS works.
Clusters
ECS clusters are logical units of core services or tasks. You can deploy containers using task definitions by first configuring a cluster. The type of cluster you configure determines the type of containers you can run in it.
You can deploy multiple clusters if you see the need. This means that for operations, you can create a Production cluster, a Testing cluster, and a Development cluster. Or you can have the clusters based on operating systems—e.g., a Linux cluster for Linux containers and a Windows cluster for Windows containers. You can use the ECS instance menu to scale your cluster up or down, or even delete it when you don’t need it anymore.
Task Definitions
Task definitions come in handy when you need to specify which Docker image should be used by a Docker container, the amount of memory to be provisioned for the container, the launch type to adopt, the network and security group configurations, the commands to run, etc. In essence, definitions control the deployment type. You can run a task definition as a task or service, depending on your preference.
Tasks
Task definitions can be used to define multiple containers. However, if you want to run and/or deploy a single container without advanced features like load balancing etc., you would use a task.
Services
Services, on the other hand, are used to run and/or deploy multiple containers that can have advanced features like load balancing. Services are created via task definitions; they run and maintain the needed number of tasks in parallel.
Containers and Images
Containers and images are synonymous with their counterparts in Docker. Your cluster is configured to run in a container environment, making the container the standard system with everything your application needs to run. Containers are created via templates called images.
Container Agents
Container agents are responsible for connecting container instances to your ECS cluster.
Launch types, or instance launch types, are hardware environments based on the host computer where your application is deployed. The launch types are responsible for managing the compute resources dedicated to deploying your application. Amazon ECS supports two launch types for running containers, Amazon EC2 and AWS Fargate.
The AWS EC2 launch type allows more control but requires more management. When using the EC2 launch type, you still need to specify the underlying infrastructure details, the launch instance, the network security setting, and the auto-scaling group if applicable. Users need to have a deep understanding of their application requirements to be able to determine the right instance type to use, and when to scale them.
Let’s explore how to create a cluster using the EC2 launch type. Make sure you are set up for Amazon ECS before proceeding. Navigate to the new AWS ECS Console and select “Clusters” in the left navigation bar. Click “Create cluster” on the following page and choose the EC2 Linux + Networking cluster template. Click “Next”:
Figure 1: Selecting a cluster template
Now, add a name for your cluster. Under “Instance configuration,” select “On-Demand Instance.” You would need to select the t2.large instance type for EC2 whether you select Spot or On-Demand.
You can leave the default settings for the rest of the section, unless you require more than the default values. To define a key pair, click the “Key Pair” dropdown menu and select any of those listed.
Figure 2: Instance configuration for EC2 cluster
Now, under “Networking,” select a VPC in the bar labeled “VPC.” Choose one or two subnets from the Subnets bar and leave everything as is.
For the container instance role, you can leave the default state. If you want to enable CloudWatch for the container, then tick on the “Enable Container Insights” button at the bottom of the page.
Click “Create” to create the cluster instance; it will take just a few minutes:
Figure 3: Completed EC2 cluster creation
If you click “View Cluster,” you should see the details about the cluster running. Clicking the “ECS Instance” status bar will show you the active instance. Remember to turn the cluster off to avoid being billed for it:
Figure 4: ECS EC2 Cluster status: Active
The AWS Fargate launch type is the serverless service for running containerized application workloads. You don’t need to worry about the underlying infrastructure; creating, configuring, and scaling clusters; or container host. These configurations are made and managed by AWS, but you have to specify which operating system to use, the CPU and memory capacity needed to package your application, the network security setting, and IAM policies. In terms of cost, you only have to pay for the virtual CPU and memory resources that are consumed in running your tasks.
The AWS Fargate architecture follows the flow in the image below:
Figure 5: The AWS Fargate launch type architecture
Creating an ECS cluster using the Fargate launch type is similar to the EC2 launch type.
First, visit the AWS ECS management console, then click “Create new cluster.” The difference is that you’re leaving the Infrastructure section as-is, without ticking the Amazon EC2 instances checkbox if you’re using the New ECS Experience, or selecting the Networking only option if you’re using the normal experience:
Figure 6: Creating a new cluster with AWS Fargate launch type
If you view the cluster in AWS CloudFormation, you will see that it has been created:
Figure 7: AWS Fargate cluster completed
Notice that you don’t have to bother with all the networking, patching, and memory allotment; AWS handles everything. If you click through to the “Infrastructure” section, you will see the capacity provider and type is Fargate:
Figure 8: Cluster was provisioned by AWS Fargate
Now, let’s compare the EC2 and Fargate launch types, focusing on when to use each.
AWS Fargate is the recommended deployment launch type in AWS, especially for those just getting started with containers and lack the expertise to manage cloud infrastructure. It abstracts away many complications and decision processes, making it faster and easier compared to EC2. As a result, if you have an unusually light workload—like a test environment—then you should run it on Fargate. Likewise, if your workload is only run in batches, like a cron job, you should also use Fargate.
The benefits of using AWS Fargate for deployments include:
AWS Fargate does have limitations:
AWS EC2 gives you the ability to control and manage the ECS cluster. It is only recommended when you need more control and the advanced features that it provides. If you wish to provision multiple CPU cores and large memory capacities—with the budget set for these features—then you should consider running the cluster on the AWS EC2 launch type. If you want to run a machine learning model, EC2 is the right choice.
AWS EC2 has the following advantages over Fargate:
The limitations of EC2 include:
The Amazon ECS service launch types have specific ideal use cases, depending on your needs. We explored when best to adopt each, and how to create clusters using your preferred option. We also discussed the use of EC2 for general applications, and Fargate for serverless applications.
If you run a microservice application, adopting an observability tool is important in order to monitor the health and activity in your containerized environment. Lumigo is a great choice for doing so; it is simple to set up and helps everyone that is part of the software development lifecycle by adding observability to their systems.