How To Create An ECS Cluster Using AWS CLI

How To Create An ECS Cluster Using AWS CLI

How To Create An ECS Cluster Using AWS CLI

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last AWS blog post, we have discussed Elastic Container Service (ECS) in AWS.

https://cloudaffaire.com/elastic-container-service-ecs/

In this blog post, we will discuss the Elastic Container Service Cluster. We will also create an ECS Cluster using AWS CLI.

What is an ECS Cluster?

AWS ECS cluster is a logical grouping of tasks or services. If you are running tasks or services that use the EC2 launch type, a cluster is also a grouping of container instances. If you are using capacity providers, a cluster is also a logical grouping of capacity providers. When you first use Amazon ECS, a default cluster is created for you, but you can create multiple clusters in an account to keep your resources separate.

ECS Cluster Capacity Providers:

ECS cluster capacity providers determine the infrastructure to use for your tasks. Each cluster has one or more capacity providers and an optional default capacity provider strategy. The capacity provider strategy determines how the tasks are spread across the capacity providers. When you run a task or create a service, you may either use the cluster’s default capacity provider strategy or specify a capacity provider strategy that overrides the cluster’s default strategy.

Cluster Capacity Provider Concepts:

Capacity provider:

A capacity provider is used in association with a cluster to determine the infrastructure that a task runs on. One or more capacity providers are specified in a capacity provider strategy, which is then associated with a cluster.

For Amazon ECS on AWS Fargate users, the FARGATE and FARGATE_SPOT capacity providers are provided automatically.

For Amazon ECS on Amazon EC2 users, a capacity provider consists of a name, an Auto Scaling group, and the settings for managed scaling and managed termination protection.

Capacity provider strategy:

A capacity provider strategy gives you control over how your tasks use one or more capacity providers. When you run a task or create a service, you specify a capacity provider strategy. A capacity provider strategy consists of one or more capacity providers with an optional base and weight specified for each provider.

The base value designates how many tasks, at a minimum, to run on the specified capacity provider. Only one capacity provider in a capacity provider strategy can have a base defined.

The weight value designates the relative percentage of the total number of launched tasks that should use the specified capacity provider. For example, if you have a strategy that contains two capacity providers, and both have a weight of 1, then when the base is satisfied, the tasks will be split evenly across the two capacity providers.

Default capacity provider strategy:

A default capacity provider strategy is associated with each Amazon ECS cluster. This determines the capacity provider strategy the cluster will use if no other capacity provider strategy or launch type is specified when running a task or creating a service.

ECS Cluster Auto Scaling:

ECS cluster auto-scaling enables you to have more control over how you scale tasks within a cluster. Amazon ECS capacity providers use Auto Scaling groups to manage the Amazon EC2 instances registered to their clusters. When creating a capacity provider, you can optionally enable managed scaling. When managed scaling is enabled, Amazon ECS manages the scale-in and scale-out actions of the Auto Scaling group. On your behalf, Amazon ECS creates an AWS Auto Scaling scaling plan with a target tracking scaling policy based on the target capacity value you specify. Amazon ECS then associates this scaling plan with your Auto Scaling group. If managed termination protection is enabled when you create a capacity provider, the Auto Scaling group and each Amazon EC2 instance in the Auto Scaling group must have instance protection from scale in enabled as well.

Note: When using managed termination protection, managed scaling must also be used otherwise managed termination protection will not work.

AWS Fargate Capacity Providers:

Amazon ECS cluster capacity providers enable you to use both Fargate and Fargate Spot capacity with your Amazon ECS tasks. With Fargate Spot you can run interruption tolerant Amazon ECS tasks at a discounted rate compared to the Fargate price. Fargate Spot runs tasks on spare compute capacity. When AWS needs the capacity back, your tasks will be interrupted with a two-minute warning.

Note: The Fargate and Fargate Spot capacity providers do not need to be created. They are available to all accounts and only need to be associated with a cluster to be available for use.

ECS Custer State:

  • ACTIVE: The cluster is ready to accept tasks and, if applicable, you can register container instances with the cluster.
  • PROVISIONING: The cluster has capacity providers associated with it and the resources needed for the capacity provider are being created.
  • DEPROVISIONING: The cluster has capacity providers associated with it and the resources needed for the capacity provider are being deleted.
  • FAILED: The cluster has capacity providers associated with it and the resources needed for the capacity provider have failed to create.
  • INACTIVE: The cluster has been deleted. Clusters with an INACTIVE status may remain discoverable in your account for a period of time.

ECS Cluster Features:

  • Clusters are Region-specific
  • A cluster may contain a mix of tasks using either the Fargate or EC2 launch types.
  • A cluster may contain a mix of both Auto Scaling group capacity providers and Fargate capacity providers, however when specifying a capacity provider strategy they may only contain one or the other but not both.
  • For tasks using the EC2 launch type, clusters can contain multiple different container instance types, but each container instance may only be registered to one cluster at a time.
  • Custom IAM policies may be created to allow or restrict user access to specific clusters.
  • When you specify a capacity provider strategy, the number of capacity providers that can be specified is limited to six.
  • A cluster may contain a mix of both Auto Scaling group capacity providers and Fargate capacity providers, however when specifying a capacity provider strategy they may only contain one or the other but not both.
  • A cluster may contain a mix of tasks and services using both capacity providers and launch types. A service may also be updated to use a capacity provider strategy rather than a launch type, however you must force a new deployment when doing so.
  • When you specify a capacity provider strategy, the base value is only supported when running tasks. When creating a service, the capacity provider strategy base parameter is not supported.
  • When using managed termination protection, managed scaling must also be used otherwise managed termination protection will not work.
  • It is recommended that you create a new Auto Scaling group to use with a capacity provider rather than using an existing one.
  • An Auto Scaling group must have a MaxSize greater than zero to scale out.
  • Managed scaling is only supported in Regions that AWS Auto Scaling is available in.
  • When using managed termination protection, managed scaling must also be used otherwise managed termination protection will not work.
  • If managed termination protection is enabled when you create a capacity provider, the Auto Scaling group and each Amazon EC2 instance in the Auto Scaling group must have instance protection from scale in enabled as well.
  • If managed scaling is enabled when you create a capacity provider, the Auto Scaling group desired count can be set to 0.
  • The Fargate and Fargate Spot capacity providers do not need to be created. They are available to all accounts and only need to be associated with a cluster to be available for use.
  • When a new cluster is created using the Amazon ECS console along with the Networking only cluster template, the FARGATE and FARGATE_SPOT capacity providers are associated with the new cluster automatically.
  • To add the FARGATE and FARGATE_SPOT capacity providers to an existing cluster, you must use the AWS CLI or API.
  • Using Fargate Spot requires that your task use platform version 1.3.0 or later.
  • When tasks using the Fargate and Fargate Spot capacity providers are stopped, a task state change event is sent to Amazon EventBridge. The stopped reason describes the cause.

Next, we are going to create an ECS Cluster using AWS CLI.

How To Create An ECS Cluster Using AWS CLI:

Step 1: Create an ECS Cluster with Fargate launch type.

Step 2: Create a task definition to run tasks and services in your ECS Cluster.

Step 3: Create a service using the task definition created in step 2.

Step 4: Get details of your ECS Cluster using AWS CLI.

Step 5: Check your application running in the ECS Cluster.

Step 6: Cleanup.

Hope you have enjoyed this article, In the next blog post, we will discuss ECS Task Definitions.

All the public cloud providers are changing the console user interface rapidly and due to this some of the screenshots used in our previous AWS blogs are no longer relevant. Hence, we have decided that from now onwards most of the demo will be done programmatically. Let us know your feedback on this in the comment section.

To get more details on AWS ECS, please refer below AWS documentation

https://docs.aws.amazon.com/ecs/index.html

 

Leave a Reply

Close Menu