ECS-(Amazon Elastic Container Service)

Ranadheer raju D
10 min readApr 8, 2020

--

Amazon Elastic Container Service(ECS) is a service which makes things easier for us to run, stop and manage Docker containers on a cluster also it is highly scalable and fast performance, where Docker Container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another and you can host your cluster on a serverless infrastructure that is managed by Amazon ECS by launching your services or tasks using the Fargate launch type, where Serverless Infrastructure is a cloud computing execution model in which the cloud provider runs the server, and dynamically manages the allocation of machine resources. It can simplify the process of deploying code into production. For more control you can host your tasks on a cluster of Amazon Elastic Compute Cloud (Amazon EC2) instances that you manage by using the EC2 launch type.

Amazon ECS launch type determines the type of infrastructure on which our tasks and services are hosted.

Fargate Launch Type

It helps us to run our containerized applications without the need to provision and manage backend infrastructure so what we need to do is just we should register our task definition and the fargate automatically launches the best container for us based on the needs that we have provided.

This diagram shows the general architecture:

For more information about Amazon ECS with AWS Fargate, see Amazon ECS on AWS Fargate.

EC2 Launch Type

The EC2 launch type allows you to run your containerized applications on a cluster of Amazon EC2 instances that you manage.

This diagram shows the general architecture:

In Amazon ECS there are some API calls using which we can launch and stop container based applications whenever we required and these API calls allows us to get the state of your cluster from a centralized service, and gives you access to many familiar Amazon EC2 features.

You can use Amazon ECS to schedule the placement of containers across your cluster based on your resource needs, isolation policies, and availability requirements. Amazon ECS eliminates the need for you to operate your own cluster management and configuration management systems or worry about scaling your management infrastructure

Amazon ECS can be used to create a consistent deployment and build experience, manage, and scale batch and Extract-Transform-Load (ETL) workloads, and build sophisticated application architectures on a microservices model.

What is Microservice model ?

Microservices are an architectural and organizational approach to software development where software is composed of small independent services that communicate over well-defined APIs. These services are owned by small, self-contained teams.

Microservices architectures make applications easier to scale and faster to develop, enabling innovation and accelerating time-to-market for new features.

There are two types of architectures that we should discuss in microservices:

Monolithic vs Microservices Architecture

With monolithic architectures, all processes are tightly coupled and run as a single service. This means that if one process of the application experiences a spike in demand, the entire architecture must be scaled, and if we add any monolithic application features then it becomes more complex as the code base grows. This complexity limits experimentation and makes it difficult to implement new ideas. There is risk for monolithic application because of many dependent tightly coupled processes increase the impact of a single process failure.

With a microservices architecture, an application is built as independent components that run each application process as a service. These services communicate via a well-defined interface using lightweight APIs. Services are built for business capabilities and each service performs a single function. Because they are independently run, each service can be updated, deployed, and scaled to meet demand for specific functions of an application.

Characteristics of microservices are: Autonomous and Specialized.

Benefits of microservices are: Agility, Flexible Scaling, Easy Deployment, Technological Freedom, Reusable code and Resilience.

We have another service called Amazon Elastic Beanstalk, this can also be used to develop, test and deploy Docker containers in conjunction with other components of your application infrastructure.

Amazon Elastic Beanstalk

With Elastic Beanstalk, you can quickly deploy and manage applications in the AWS Cloud without having to learn about the infrastructure that runs those applications.

Elastic Beanstalk reduces management complexity without restricting choice or control. You simply upload your application, and Elastic Beanstalk automatically handles the details of capacity provisioning, load balancing, scaling, and application health monitoring.

Elastic Beanstalk supports applications developed in Go, Java, .NET, Node.js, PHP, Python, and Ruby.

The following diagram illustrates the workflow of Elastic Beanstalk.

Features of Amazon ECS

NOTE: Amazon ECS is a regional service.

Because it is a regional service it simplifies the application containers in a highly available manner across multiple Avalability Zones within a Region and we can create Amazon ECS clusters within a new or existing VPC which is already created.

We can define the task definitions and services only when the cluster is up and in running state. After a cluster is up and running, you can define task definitions and services that specify which Docker container images to run across your clusters. Container images are stored in and pulled from container registries, which may exist within or outside of your AWS infrastructure.

The following diagram shows the architecture of an Amazon ECS environment using the Fargate launch type:

Now let us discuss this architecture in more detail.

Containers and Images

To deploy or run any application in containers, the application components must be architected. In the starting of this article we already discussed about the definition of docker container definition, so I am repeating the same here, a Docker container is a standardized unit of software development, containing everything that your software application needs to run: code, runtime, system tools, system libraries, etc.

One thing we should remember is that containers are created from read-only template which is also called as Image.

The images are built from a docker file, a plain text file that specifies all of the components that are included in the container. These images are then stored in a registry from which they can be downloaded and run on your cluster.

To understand deeply about container technology, first we should have a knowledge on docker so let’s discuss about that just for understanding purpose.

Docker is a technology that allows you to build, run, test, and deploy distributed applications that are based on Linux containers. Amazon ECS uses Docker images in task definitions to launch containers on Amazon EC2 instances in your clusters.

Task Definitions

Task definition is mandatory to run any application on Amazon ECS, except task definition remaining everything will be taken care by Amazon ECS. The task definition format is either in a text file or JSON format which describes one or more containers, the limit of creating containers is up to maximum of 10 that form your application also we can say that it is a blue print for your application.

There are different parameters that task definition will specify for your application.

  • The Docker image to use with each container in your task
  • How much CPU and memory to use with each task or each container within a task
  • The launch type to use, which determines the infrastructure on which your tasks are hosted
  • The Docker networking mode to use for the containers in your task
  • The logging configuration to use for your tasks
  • Whether the task should continue to run if the container finishes or fails
  • The command the container should run when it is started
  • Any data volumes that should be used with the containers in the task
  • The IAM role that your tasks should use

You can define multiple containers in a task definition. The parameters that you use depend on the launch type you choose for the task. Not all parameters are valid.

Your entire application stack does not need to exist on a single task definition, and in most cases it should not. Your application can span multiple task definitions by combining related containers into their own task definitions, each representing a single component.

The following is an example of a task definition containing a single container that runs an NGINX web server using the Fargate launch type.

{
"family": "webserver",
"containerDefinitions": [
{
"name": "web",
"image": "nginx",
"memory": "100",
"cpu": "99"
},
],
"requiresCompatibilities": [
"FARGATE"
],
"networkMode": "awsvpc",
"memory": "512",
"cpu": "256",
}

Below is a list of additional task definition examples:

  1. Webserver
  2. Wordpress and MySQL
  3. AWSlogs Log Driver
  4. Splunk Log Driver
  5. Fluentd Log Driver
  6. Gelf Log Driver
  7. Amazon ECR Image and task definition IAM role
  8. Entrypoint with command
  9. Container Dependency

Tasks and Scheduling

After a successful creation of a task definition for your application we can specify, how many number of tasks that you need to run on you are cluster.

Each task that uses the Fargate launch type has its own isolation boundary and does not share the underlying kernel, CPU resources, memory resources, or elastic network interface with another task.

The Amazon ECS task scheduler is responsible for placing tasks within your cluster. There are several different scheduling options available. For example, you can define a service that runs and maintains a specified number of tasks simultaneously.

Clusters

Till now we have discussed so many times about cluster but we donno what exactly means a cluster, so now we will look into the cluster definition and some description about cluster.

A cluster is a logical grouping of resources. When using the Fargate launch type with tasks within your cluster, Amazon ECS manages your cluster resources. When using the EC2 launch type, then your clusters are a group of container instances you manage. An Amazon ECS container instance is an Amazon EC2 instance that is running the Amazon ECS container agent. Amazon ECS downloads your container images from a registry that you specify, and runs those images within your cluster.

Container Agent

The container agent runs on each infrastructure resource within an Amazon ECS cluster. It sends information about the resource’s current running tasks and resource utilization to Amazon ECS, and starts and stops tasks whenever it receives a request from Amazon ECS.

To know more about container agent visit here container agent info.

How to Get Started with Amazon ECS

If you are a new to Amazon ECS then it is highly recommend to follow these instructions, Amazon ECS provides a first-run wizard that steps you through defining a task definition for a web server, configuring a service, and launching your first Fargate task. The first-run wizard is highly recommended for users who have no prior experience with Amazon ECS.

Also we can use ECS through AWS CLI. Click here to know more about this.

Amazon ECS can be used along with the following AWS services:

Related Services

AWS Identity and Access Management

In Amazon ECS, IAM can be used to control access at the container instance level using IAM roles, and at the task level using IAM task roles.

Amazon EC2 Auto Scaling

You can use Auto Scaling with a Fargate task within a service to scale in response to a number of metrics or with an EC2 task to scale the container instances within your cluster.

Elastic Load Balancing

You can use Elastic Load Balancing to create an endpoint that balances traffic across services in a cluster.

Amazon Elastic Container Registry

Amazon ECR is a managed AWS Docker registry service that is secure, scalable, and reliable. Amazon ECR supports private Docker repositories with resource-based permissions using IAM so that specific users or tasks can access repositories and images. Developers can use the Docker CLI to push, pull, and manage images.

AWS Cloudformation

You can define clusters, task definitions, and services as entities in an AWS CloudFormation script.

How to Access Amazon ECS

Amazon Management Console

The console is a browser-based interface to manage Amazon ECS resources.

Amazon CLI

You can use the AWS command line tools to issue commands at your system’s command line to perform Amazon ECS and AWS tasks; this can be faster and more convenient than using the console. The command line tools are also useful for building scripts that perform AWS tasks.

Amazon ECS CLI

which provides high-level commands to simplify creating, updating, and monitoring clusters and tasks from a local development environment using Docker Compose.

AWS SDK’s

We also provide SDKs that enable you to access Amazon ECS from a variety of programming languages. The SDKs automatically take care of tasks such as:

  • Cryptographically signing your service requests
  • Retrying requests
  • Handling error responses

Amazon ECS Use Cases

Microservices

Containers provide process isolation that makes it easy to break apart and run applications as independent components called microservices.

Batch Processing

Package batch processing and ETL jobs into containers to start jobs quickly and scale them dynamically in response to demand.

Machine Learning

Use containers to quickly scale machine learning models for training and inference and run them close to your data sources on any platform.

Hybrd Applications

Containers let you standardize how code is deployed, making it easy to build workflows for applications that run between on-premises and cloud environments.

Application Migration to the Cloud

Containers make it easy to package entire applications and move them to the cloud without needing to make any code changes.

Platform as a service

Use containers to build platforms that remove the need for developers to manage infrastructure and standardize how your applications are deployed and managed.

Thank you!!

--

--

Ranadheer raju D
Ranadheer raju D

Written by Ranadheer raju D

Software Development Engineer | AWS Solution Architect | Piazza Tech Consulting Group