ECS on Fargate: The Greatest Services to Run Your Containers

ECS on Fargate: The Greatest Services to Run Your Containers

Amazon Elastic Container Service (ECS) together with AWS Fargate is a powerful combination of Amazon's container orchestration service and serverless compute engine that simplifies the management and deployment of containerized applications. ECS is one of the most used compute services in AWS.

In this article, we will explore the fundamentals of containers, the need for orchestration, and the benefits of using ECS with Fargate to run your applications seamlessly across different computing environments.

Amazon ECS Infographic

Whether you're experienced, just starting out, or have never heard of ECS, take a look at our infographic that covers all the essential information about Amazon ECS.

Amazon ECS - A fully managed container orchestration service.

Introduction

Before diving headfirst into the depths of Amazon's container orchestration service, it is essential to establish a strong foundation by thoroughly understanding the fundamental concepts and principles that underpin this powerful technology.

What Are Containers?

Containers are a way of packaging software applications, along with all their dependencies, into a single image that can be run consistently across different computing environments. Containers allow developers to create an application once, and then run it anywhere, without having to worry about the underlying infrastructure.

Docker is a popular open-source platform for building and deploying containers. Docker provides a simple and efficient way to create, manage, and run containers, making it easier for developers to build and deploy applications. Docker uses a layered file system to store container images, which makes them lightweight and easy to share.

Containers are the core building block for many applications.

With Docker, developers can package their applications into containers, along with all the necessary dependencies, and then deploy them to any environment that supports Docker. Docker also provides a range of tools for managing containers, including Docker Compose, which allows developers to define and run multi-container applications, and Docker Swarm, which provides orchestration and scaling capabilities for containerized applications.

What Is Container Orchestration and Why Do We Need It?

Container orchestration is the process of managing and coordinating the deployment, scaling, and operation of containerized applications. Container orchestration is necessary because as the number of containers and containerized applications grows, it becomes increasingly difficult to manage them manually.

Container orchestration provides a way to automate the management of containers and containerized applications, making it easier to deploy, scale, and manage them.

Here are some reasons why we need container orchestration:

  1. Elasticity: Container orchestration makes it easy to scale containerized applications vertically or horizontally, depending on the demand. This allows organizations to handle increased traffic or workload without downtime or performance issues. With this approach, we ensure that we strike a balance between computing resources and expenses.

  2. High availability: Container orchestration provides features such as load balancing, automatic failover, and self-healing, which ensure that containerized applications are always available, even in the event of a failure in an availability zone or even a complete region outage.

  3. Automation: Container orchestration automates many of the manual tasks involved in managing containers and containerized applications, allowing organizations to focus on developing and deploying applications, instead of running and operating them.

Overall, container orchestration is essential for managing the complexity of containerized applications, ensuring their availability and reliability, and optimizing resource utilization.

What is ECS?

We've explored what containers are and why we need orchestration services.

Now, let's delve into Amazon ECS and the fundamental concepts behind one of the most mature core services offered by AWS.

ECS - An Orchestration Management Platform

ECS is a highly scalable and fast container management service. It offers a management plane to orchestrate containers of your cluster: run, stop & manage containers with ease.

ECS comes with many features to simplify your development process and reduce operations and liabilities.

  • Don’t worry about underlying infrastructure with the Fargate launch type. You’ll only determine which container image you want to run and what workload capabilities you require regarding memory or virtual CPUs.

  • It’s fully integrated with AWS IAM. You can define fine-granular permissions based on your requirements and never think about users or passwords. Define any level of isolation you want or require from a compliance perspective.

  • Automatically integrated with CloudWatch, default metrics, and log ingestion are provided, ensuring effortless monitoring of resource usage and application health.

ECS is one of the most battle-tested services of AWS and often a perfect fit for critical core infrastructure that needs to handle high-volume request microservice APIs.

As we believe in a cloud-native future, we’ll only break down the basics of the EC2 launch types and not go into detail but focus on Fargate.

Key Terms and Concepts of ECS

While exploring and learning about ECS, you will encounter many key terms that are neither intuitive nor easy to comprehend initially. However, they are essential for understanding how all the components of ECS work together.

ECS key concepts: Task Definition, Task, Service, and Cluster.

Let's delve into containers, tasks and task definitions, services, and clusters.

Task Definition - The Blueprint to Run Your Containers

A task definition outlines how to launch one or multiple containers. The task definition includes the launch type, roles, container image, CPU and memory allocation, environment variables, secrets, logging configuration, and exposed ports.

  • The launch type specifies which service to use for executing tasks, either EC2, Fargate, or External.

  • Two dedicated roles are required for ECS, a task execution role, and a task role.

  • The container image is the docker image to run, residing in a container registry like Amazon's Elastic Container Registry.

  • CPU and memory allocation are the assigned compute resources, which vary depending on the launch type.

  • Environment variables are key-value pairs that inject parameters into your application.

  • Secrets can be securely injected from AWS Secrets Manager or Systems Manager Parameter Store.

  • Logging configuration defines the log driver and destination for ECS to send your logs.

  • Exposed ports are defined to map ports between ECS and your container image.

Task - A Containerized Application That is Deployed to Run on EC2 or Fargate

A task is the execution of a task definition with a set of containers on the same host. It's defined using the Docker Compose file format, specifying container images, environment variables, and port mappings. Tasks can be launched directly and will stay active until stopped or exited on their own, without any replacement.

Imagine the task as the actual instantiation of a task definition. The task definition serves as the blueprint, while the task itself represents the actual execution.

Service - Managing a Group of Tasks

A service is a persistent process that oversees a group of tasks and ensures that the desired number of tasks are continually running. If a task stops due to container errors and the number of healthy tasks drops below the threshold, ECS will automatically launch a new task.

Consider the service as the supervisor of a group of tasks, ensuring that your tasks remain in good health.

Clusters - A Logical Grouping of Container Instances

A cluster is a logical collection of services or tasks that run on registered infrastructure, which can be provided by AWS Fargate, EC2 instances managed by yourself, on-premise servers, or remotely managed virtual machines.

The Different Launch Types

We have covered the basics of containers and ECS's capacity to orchestrate them. But which service actually executes your containers? With ECS, you can select from several options.

A notable solution for this is EC2. You can deploy your EC2 instances within your clusters to execute your containers. With EC2, you maintain complete control over the underlying infrastructure, including the instance type, operating system, and security groups.

It’s a good launch type choice for workloads that require consistently high CPU and memory, workloads that are optimized for pricing, or applications with persistent storage requirements. Nonetheless, EC2 necessitates numerous manual operations since you are accountable for tasks such as keeping your operating system up-to-date.

And that's why Amazon introduced a serverless compute option for ECS based on containers: Fargate.

What is Fargate?

When running EC2 instances within an ECS cluster, ECS handles much of the orchestration burden. However, maintaining the instances, such as updating the operating system, remains our responsibility.

With Fargate, we enter a higher level of abstraction.

Fargate - A Serverless Compute Engine for Containers

Fargate is a serverless, pay-as-you-go option that eliminates the need to maintain any infrastructure. With Fargate, you simply specify the task definition, cluster, and desired number of tasks, and ECS handles everything else. Fargate automatically provisions the required compute resources, such as Amazon Elastic Compute Cloud (EC2) instances (which are entirely removed from your responsibility), and runs the tasks on those instances.

Fargate is the recommended launch type for small to large workloads that may require low overhead and experience occasional bursts. If you're unsure about your complete requirements, always opt for the Fargate launch type to minimize operations and liabilities.

The Different Lifecycle States of Your Tasks

A task progresses through various lifecycle stages, irrespective of whether it was initiated manually or as a component of a service. Amazon's ECS container agent monitors all state transitions, the most recent known state, and the intended state.

The different lifecycle states of a task in Fargate.

The lifecycle of a task includes the following states:

  • Provisioning - Preconditions are in progress, e.g. attaching the Elastic Network Interface (ENI) if the task resides in a VPC.

  • Pending - Waiting until the required resources for the task are available

  • Activating - Final steps that take place before moving to the running state, e.g. if attached to load balancing, ECS takes care of registering the task to the target groups.

  • Running - The task is successfully running

  • Deactivating - Necessary steps to take place before stopping the task, e.g. detaching from target groups if the task is part of a load balancer.

  • Stopping - Gracefully shutting down the containers via SIGTERM signals. If a container does not stop within the configured timeout, there will be a forceful shutdown via SIGKILL.

  • Deprovisioning - Final steps before transitioning to the stopped state, e.g. detaching the ENI if the task resides in a VPC.

  • Stopped - The task has been successfully stopped.

For batch jobs, the task will progress through the various states, while other strategies, such as the service scheduler, aim to maintain tasks in the running state indefinitely or scale them horizontally as required.

Benefits of using ECS with Fargate

Leveraging Amazon Elastic Container Service (ECS) in conjunction with AWS Fargate offers numerous advantages for businesses and developers alike. By combining these two powerful services, users can experience a more streamlined and efficient container management process.

Service Maturity - It just works

AWS ECS is a mature service that has been around for several years, and it has been battle-tested by startups as well as enterprise companies. It has proven to be reliable and stable: it just works.

Steep Learning Curve - It's Easy to Start With

Despite being a powerful tool, AWS ECS has a low barrier to entry. It's easy to start with, and the learning curve is very steep, making it accessible even to beginners.

Even though it may initially seem overwhelming with all the terminology such as tasks, task definitions, services, and clusters, the confusion clears up quickly, and everything becomes easy to run and operate.

Simplicity - It's Effortless to Operate

AWS ECS is designed to be simple to operate. It provides a straightforward interface that enables you to manage containers with ease, and it's effortless to set up and configure.

Many applications I've built in the past were initially set up on ECS with Fargate and never required any infrastructure updates. They never experienced outages and were running for years without any manual operations required.

Cost-Efficient - It Handles Steady Traffic Efficiently

AWS ECS is cost-efficient, especially for handling steady traffic. It allows users to optimize resource utilization and reduce infrastructure costs, making it an excellent choice for companies with tight budgets.

Even a low number of tasks with very minimal vCPU and memory settings are enough to handle most traffic requirements.

Other serverless compute options, such as Lambda, may initially be less expensive, but they can quickly become more costly than ECS with Fargate if traffic steadily increases.

Wide-Spead - Used across Startups and Enterprise Companies

AWS ECS is widely used across startups and enterprise companies. It's a popular choice for many businesses due to its reliability, scalability, and ease of use.

In the years I've worked as a software developer, as cloud computing has grown, I have always encountered projects that involved working with ECS and Fargate at some point. Frequently, mission-critical core services were running on this exceptional platform.

Provider-Independent - Painless To Migrate

With the packaged software within your container, migrating to other cloud providers or on-premises infrastructure is less painful than with other non-containerized applications, e.g. distributed microservices running on Lambda.

This enables businesses to partially avoid vendor lock-in (although, due to native integrations and provider-specific details, migrations can still be quite challenging) and maintain greater flexibility compared to other services.

Seamlessly Integrated - Smoothly Blends in with Other Services

ECS and Fargate are designed to work seamlessly with other AWS services, such as AWS Identity and Access Management (IAM), Amazon DynamoDB, Simple Queue Service (SQS), or Elastic Load Balancing (ELB).

This allows you to create and implement fully integrated, comprehensive solutions using a single cloud service provider platform.

Conclusion

In conclusion, Amazon ECS with Fargate provides a reliable, cost-efficient, and easy-to-use solution for running containerized applications.

By leveraging the maturity and simplicity of these services, businesses can focus on developing and deploying applications while enjoying the benefits of scalability, high availability, and resource optimization. Choosing ECS with Fargate as your container orchestration platform can significantly reduce operational overhead and increase the flexibility of your infrastructure.

Frequently Asked Questions

  1. What are the main benefits of using ECS with Fargate?
    The main benefits of using ECS with Fargate include service maturity, ease of use, cost-efficiency, widespread adoption, and provider independence.

  2. What is the difference between ECS and Fargate?
    ECS is a container orchestration service, while Fargate is a serverless compute engine for containers. Fargate allows you to run containers without managing the underlying infrastructure.

  3. Can I use ECS with other computing options than Fargate?
    Yes, you can use ECS with either EC2 instances or Fargate, depending on your needs and preferences for infrastructure management.

  4. Why do I need a container orchestration service?
    Container orchestration automates the management of containers, making it easier to scale applications up or down and ensuring high availability through features like load balancing, automatic failover, and self-healing.

  5. What is a task definition in ECS?
    A task definition is a blueprint that outlines how to launch one or multiple containers in ECS. It includes details such as the launch type, roles, container image, CPU and memory allocation, environment variables, secrets, logging configuration, and exposed ports.

Are you interested in more?

Head over to our bi-weekly newsletter or check out the following blog posts