Usar ListClusters com o AWS SDK ou a CLI - Exemplos de código do AWS SDK

Há mais exemplos do AWS SDK disponíveis no repositório do GitHub Documento de Exemplos do AWS SDK.

Usar ListClusters com o AWS SDK ou a CLI

Os exemplos de código a seguir mostram como usar o ListClusters.

Exemplos de ações são trechos de código de programas maiores e devem ser executados em contexto. É possível ver essa ação no contexto no seguinte exemplo de código:

.NET
SDK para .NET
nota

Há mais no GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

/// <summary> /// List cluster ARNs available. /// </summary> /// <returns>The ARN list of clusters.</returns> public async Task<List<string>> GetClusterARNSAsync() { Console.WriteLine("Getting a list of all the clusters in your AWS account..."); List<string> clusterArnList = new List<string>(); // Get a list of all the clusters in your AWS account try { var listClustersResponse = _ecsClient.Paginators.ListClusters(new ListClustersRequest { }); var clusterArns = listClustersResponse.ClusterArns; // Print the ARNs of the clusters await foreach (var clusterArn in clusterArns) { clusterArnList.Add(clusterArn); } if (clusterArnList.Count == 0) { _logger.LogWarning("No clusters found in your AWS account."); } return clusterArnList; } catch (Exception e) { _logger.LogError($"An error occurred while getting a list of all the clusters in your AWS account. {e.InnerException}"); throw new Exception($"An error occurred while getting a list of all the clusters in your AWS account. {e.InnerException}"); } }
  • Consulte detalhes da API em ListClusters na Referência da API AWS SDK para .NET.

CLI
AWS CLI

Como listar os clusters disponíveis

O exemplo de list-clusters a seguir lista todos os clusters disponíveis.

aws ecs list-clusters

Saída:

{ "clusterArns": [ "arn:aws:ecs:us-west-2:123456789012:cluster/MyECSCluster1", "arn:aws:ecs:us-west-2:123456789012:cluster/AnotherECSCluster" ] }

Para obter mais informações, consulte Clusters do Amazon ECS no Guia do desenvolvedor do Amazon ECS.

  • Consulte detalhes da API em ListClusters na Referência de comandos da AWS CLI.

Java
SDK para Java 2.x
nota

Há mais no GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.ListClustersResponse; 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 ListClusters { public static void main(String[] args) { Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); listAllClusters(ecsClient); ecsClient.close(); } public static void listAllClusters(EcsClient ecsClient) { try { ListClustersResponse response = ecsClient.listClusters(); List<String> clusters = response.clusterArns(); for (String cluster : clusters) { System.out.println("The cluster arn is " + cluster); } } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • Consulte detalhes da API em ListClusters na Referência da API AWS SDK for Java 2.x.

PowerShell
Ferramentas para PowerShell V4

Exemplo 1: esse cmdlet retorna uma lista de clusters do ECS existentes.

Get-ECSClusterList

Saída:

arn:aws:ecs:us-west-2:012345678912:cluster/LAB-ECS-CL arn:aws:ecs:us-west-2:012345678912:cluster/LAB-ECS
  • Consulte detalhes da API em ListClusters na Referência de cmdlet do Ferramentas da AWS para PowerShell (V4).

Ferramentas para PowerShell V5

Exemplo 1: esse cmdlet retorna uma lista de clusters do ECS existentes.

Get-ECSClusterList

Saída:

arn:aws:ecs:us-west-2:012345678912:cluster/LAB-ECS-CL arn:aws:ecs:us-west-2:012345678912:cluster/LAB-ECS
  • Consulte detalhes da API em ListClusters na Referência de cmdlet do Ferramentas da AWS para PowerShell (V5).