DescribeClusters 搭配 a AWS SDK 或 CLI 使用 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DescribeClusters 搭配 a AWS SDK 或 CLI 使用

下列程式碼範例示範如何使用 DescribeClusters

CLI
AWS CLI

範例 1:描述叢集

下列describe-clusters範例會擷取指定叢集的詳細資訊。

aws ecs describe-clusters \ --cluster default

輸出:

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

如需詳細資訊,請參閱 Amazon ECS 開發人員指南中的 Amazon Word 叢集 ECS

範例 2:使用連接選項描述叢集

下列describe-clusters範例指定 ATTACHMENTS 選項。它會擷取指定叢集的詳細資訊,以及以附件形式連接至叢集的資源清單。將容量提供者與叢集搭配使用時,資源,無論是 AutoScaling 計劃或擴展政策,都會以 asp 或 as_policy ATTACHMENTS 表示。

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

輸出:

{ "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": [] }

如需詳細資訊,請參閱 Amazon ECS 開發人員指南中的 Amazon Word 叢集 ECS

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DescribeClusters

Java
Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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); } } }
  • 如需 API 詳細資訊,請參閱 DescribeClusters AWS SDK for Java 2.x 參考中的 API

PowerShell
for PowerShell 工具

範例 1:此 cmdlet 說明一或多個 ECS 叢集。

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

輸出:

LoggedAt : 12/27/2019 9:27:41 PM Clusters : {LAB-ECS-CL} Failures : {} ResponseMetadata : Amazon.Runtime.ResponseMetadata ContentLength : 396 HttpStatusCode : OK
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 DescribeClusters

Rust
Rust 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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(()) }
  • 如需 API 詳細資訊,請參閱 DescribeClusters AWS for Rust SDK 參考中的 API