View a markdown version of this page

Use DescribeClusters with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use DescribeClusters with an AWS SDK or CLI

The following code examples show how to use DescribeClusters.

Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code examples:

CLI
AWS CLI

Example 1: To describe a cluster

The following describe-clusters example retrieves details about the specified cluster.

aws ecs describe-clusters \ --cluster default

Output:

{ "clusters": [ { "status": "ACTIVE", "clusterName": "default", "registeredContainerInstancesCount": 0, "pendingTasksCount": 0, "runningTasksCount": 0, "activeServicesCount": 1, "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/default" } ], "failures": [] }

For more information, see Amazon ECS Clusters in the Amazon ECS Developer Guide.

Example 2: To describe a cluster with the attachment option

The following describe-clusters example specifies the ATTACHMENTS option. It retrieves details about the specified cluster and a list of resources attached to the cluster in the form of attachments. When using a capacity provider with a cluster, the resources, either AutoScaling plans or scaling policies, will be represented as asp or as_policy ATTACHMENTS.

aws ecs describe-clusters \ --include ATTACHMENTS \ --clusters sampleCluster

Output:

{ "clusters": [ { "clusterArn": "arn:aws:ecs:af-south-1:123456789222:cluster/sampleCluster", "clusterName": "sampleCluster", "status": "ACTIVE", "registeredContainerInstancesCount": 0, "runningTasksCount": 0, "pendingTasksCount": 0, "activeServicesCount": 0, "statistics": [], "tags": [], "settings": [], "capacityProviders": [ "sampleCapacityProvider" ], "defaultCapacityProviderStrategy": [], "attachments": [ { "id": "a1b2c3d4-5678-901b-cdef-EXAMPLE22222", "type": "as_policy", "status": "CREATED", "details": [ { "name": "capacityProviderName", "value": "sampleCapacityProvider" }, { "name": "scalingPolicyName", "value": "ECSManagedAutoScalingPolicy-3048e262-fe39-4eaf-826d-6f975d303188" } ] } ], "attachmentsStatus": "UPDATE_COMPLETE" } ], "failures": [] }

For more information, see Amazon ECS Clusters in the Amazon ECS Developer Guide.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.DescribeClustersRequest; import software.amazon.awssdk.services.ecs.model.DescribeClustersResponse; import software.amazon.awssdk.services.ecs.model.Cluster; import software.amazon.awssdk.services.ecs.model.EcsException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DescribeClusters { public static void main(String[] args) { final String usage = """ Usage: <clusterArn> \s Where: clusterArn - The ARN of the ECS cluster to describe. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String clusterArn = args[0]; Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); descCluster(ecsClient, clusterArn); } public static void descCluster(EcsClient ecsClient, String clusterArn) { try { DescribeClustersRequest clustersRequest = DescribeClustersRequest.builder() .clusters(clusterArn) .build(); DescribeClustersResponse response = ecsClient.describeClusters(clustersRequest); List<Cluster> clusters = response.clusters(); for (Cluster cluster : clusters) { System.out.println("The cluster name is " + cluster.clusterName()); } } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
PowerShell
Tools for PowerShell V4

Example 1: This cmdlet describes one or more of your ECS clusters.

Get-ECSClusterDetail -Cluster "LAB-ECS-CL" -Include SETTINGS | Select-Object *

Output:

LoggedAt : 12/27/2019 9:27:41 PM Clusters : {LAB-ECS-CL} Failures : {} ResponseMetadata : Amazon.Runtime.ResponseMetadata ContentLength : 396 HttpStatusCode : OK
  • For API details, see DescribeClusters in AWS Tools for PowerShell Cmdlet Reference (V4).

Tools for PowerShell V5

Example 1: This cmdlet describes one or more of your ECS clusters.

Get-ECSClusterDetail -Cluster "LAB-ECS-CL" -Include SETTINGS | Select-Object *

Output:

LoggedAt : 12/27/2019 9:27:41 PM Clusters : {LAB-ECS-CL} Failures : {} ResponseMetadata : Amazon.Runtime.ResponseMetadata ContentLength : 396 HttpStatusCode : OK
  • For API details, see DescribeClusters in AWS Tools for PowerShell Cmdlet Reference (V5).

Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

class EcsWrapper: """Encapsulates Amazon ECS operations.""" def __init__(self, ecs_client: BaseClient): """ Initializes the EcsWrapper with an ECS client. :param ecs_client: A Boto3 Amazon ECS client. Boto3 clients are created by the ``boto3.client`` factory function and are instances of ``botocore.client.BaseClient``, which is the correct type to annotate here (``boto3.client`` itself is a function, not a type). """ self.ecs_client = ecs_client @classmethod def from_client(cls) -> "EcsWrapper": """Creates an EcsWrapper using a default Boto3 ECS client.""" ecs_client = boto3.client("ecs") return cls(ecs_client) def describe_clusters(self, cluster_names: List[str]) -> List[Dict[str, Any]]: """ Describes one or more ECS clusters. :param cluster_names: A list of cluster names or ARNs. :return: A list of cluster details. :raises ClientError: If the request fails (e.g., ClusterNotFoundException). """ try: response = self.ecs_client.describe_clusters( clusters=cluster_names, include=["STATISTICS"], ) clusters = response.get("clusters", list()) for cluster in clusters: logger.info( "Cluster '%s': status=%s, active_services=%s, running_tasks=%s", cluster["clusterName"], cluster["status"], cluster["activeServicesCount"], cluster["runningTasksCount"], ) return clusters except ClientError as err: if err.response["Error"]["Code"] == "ClusterNotFoundException": logger.error( "Cluster(s) not found: %s. %s", cluster_names, err.response["Error"]["Message"], ) raise
  • For API details, see DescribeClusters in AWS SDK for Python (Boto3) API Reference.

Rust
SDK for Rust
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

async fn show_clusters(client: &aws_sdk_ecs::Client) -> Result<(), aws_sdk_ecs::Error> { let resp = client.list_clusters().send().await?; let cluster_arns = resp.cluster_arns(); println!("Found {} clusters:", cluster_arns.len()); let clusters = client .describe_clusters() .set_clusters(Some(cluster_arns.into())) .send() .await?; for cluster in clusters.clusters() { println!(" ARN: {}", cluster.cluster_arn().unwrap()); println!(" Name: {}", cluster.cluster_name().unwrap()); } Ok(()) }