ECSExemplos da Amazon usando AWS SDK for .NET - AWS SDK for .NET

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

ECSExemplos da Amazon usando AWS SDK for .NET

Os exemplos de código a seguir mostram como realizar ações e implementar cenários comuns usando o AWS SDK for .NET com a AmazonECS.

Ações são trechos de código de programas maiores e devem ser executadas em contexto. Embora as ações mostrem como chamar funções de serviço individuais, você pode ver as ações no contexto em seus cenários relacionados.

Os cenários são exemplos de código que mostram como realizar tarefas específicas chamando várias funções dentro de um serviço ou combinadas com outros serviços da AWS.

Cada exemplo inclui um link para o código-fonte completo, onde você pode encontrar instruções sobre como configurar e executar o código no contexto.

Conceitos básicos

O exemplo de código a seguir mostra como começar a usar a AmazonECS.

AWS SDK for .NET
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

using Amazon.ECS; using Amazon.ECS.Model; using Microsoft.Extensions.Hosting; namespace ECSActions; public class HelloECS { static async System.Threading.Tasks.Task Main(string[] args) { // Use the AWS .NET Core Setup package to set up dependency injection for the Amazon ECS domain registration service. // Use your AWS profile name, or leave it blank to use the default profile. using var host = Host.CreateDefaultBuilder(args).Build(); // Now the client is available for injection. var amazonECSClient = new AmazonECSClient(); // You can use await and any of the async methods to get a response. var response = await amazonECSClient.ListClustersAsync(new ListClustersRequest { }); Console.WriteLine($"Hello Amazon ECS! Following are some cluster ARNS available in the your aws account"); Console.WriteLine(); foreach (var arn in response.ClusterArns.Take(5)) { Console.WriteLine($"\tARN: {arn}"); Console.WriteLine($"Cluster Name: {arn.Split("/").Last()}"); Console.WriteLine(); } } }
  • Para API obter detalhes, consulte ListClustersem AWS SDK for .NET APIReferência.

Ações

O código de exemplo a seguir mostra como usar ListClusters.

AWS SDK for .NET
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

/// <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}"); } }
  • Para API obter detalhes, consulte ListClustersem AWS SDK for .NET APIReferência.

O código de exemplo a seguir mostra como usar ListServices.

AWS SDK for .NET
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

/// <summary> /// List service ARNs available. /// </summary> /// <param name="clusterARN">The arn of the ECS cluster.</param> /// <returns>The ARN list of services in given cluster.</returns> public async Task<List<string>> GetServiceARNSAsync(string clusterARN) { List<string> serviceArns = new List<string>(); var request = new ListServicesRequest { Cluster = clusterARN }; // Call the ListServices API operation and get the list of service ARNs var serviceList = _ecsClient.Paginators.ListServices(request); await foreach (var serviceARN in serviceList.ServiceArns) { if (serviceARN is null) continue; serviceArns.Add(serviceARN); } if (serviceArns.Count == 0) { _logger.LogWarning($"No services found in cluster {clusterARN} ."); } return serviceArns; }
  • Para API obter detalhes, consulte ListServicesem AWS SDK for .NET APIReferência.

O código de exemplo a seguir mostra como usar ListTasks.

AWS SDK for .NET
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

/// <summary> /// List task ARNs available. /// </summary> /// <param name="clusterARN">The arn of the ECS cluster.</param> /// <returns>The ARN list of tasks in given cluster.</returns> public async Task<List<string>> GetTaskARNsAsync(string clusterARN) { // Set up the request to describe the tasks in the service var listTasksRequest = new ListTasksRequest { Cluster = clusterARN }; List<string> taskArns = new List<string>(); // Call the ListTasks API operation and get the list of task ARNs var tasks = _ecsClient.Paginators.ListTasks(listTasksRequest); await foreach (var task in tasks.TaskArns) { if (task is null) continue; taskArns.Add(task); } if (taskArns.Count == 0) { _logger.LogWarning("No tasks found in cluster: " + clusterARN); } return taskArns; }
  • Para API obter detalhes, consulte ListTasksem AWS SDK for .NET APIReferência.

Cenários

O exemplo de código a seguir mostra como:

  • Obter uma lista de todos os clusters.

  • Obter serviços para um cluster.

  • Obter tarefas para um cluster.

AWS SDK for .NET
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

Execute um cenário interativo em um prompt de comando.

using Amazon.ECS; using ECSActions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Microsoft.Extensions.Logging.Debug; namespace ECSScenario; public class ECSScenario { /* Before running this .NET code example, set up your development environment, including your credentials. This .NET example performs the following tasks: 1. List ECS Cluster ARNs. 2. List services in every cluster 3. List Task ARNs in every cluster. */ private static ILogger logger = null!; private static ECSWrapper _ecsWrapper = null!; static async Task Main(string[] args) { // Set up dependency injection for the Amazon service. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .Build(); ILoggerFactory loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger<ECSScenario>(); var loggerECSWarpper = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger<ECSWrapper>(); var amazonECSClient = new AmazonECSClient(); _ecsWrapper = new ECSWrapper(amazonECSClient, loggerECSWarpper); Console.WriteLine(new string('-', 80)); Console.WriteLine("Welcome to the Amazon ECS example scenario."); Console.WriteLine(new string('-', 80)); try { await ListClusterARNs(); await ListServiceARNs(); await ListTaskARNs(); } catch (Exception ex) { logger.LogError(ex, "There was a problem executing the scenario."); } } /// <summary> /// List ECS Cluster ARNs /// </summary> private static async Task ListClusterARNs() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"1. List Cluster ARNs from ECS."); var arns = await _ecsWrapper.GetClusterARNSAsync(); foreach (var arn in arns) { Console.WriteLine($"Cluster arn: {arn}"); Console.WriteLine($"Cluster name: {arn.Split("/").Last()}"); } Console.WriteLine(new string('-', 80)); } /// <summary> /// List services in every cluster /// </summary> private static async Task ListServiceARNs() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"2. List Service ARNs in every cluster."); var clusterARNs = await _ecsWrapper.GetClusterARNSAsync(); foreach (var clusterARN in clusterARNs) { Console.WriteLine($"Getting services for cluster name: {clusterARN.Split("/").Last()}"); Console.WriteLine(new string('.', 5)); var serviceARNs = await _ecsWrapper.GetServiceARNSAsync(clusterARN); foreach (var serviceARN in serviceARNs) { Console.WriteLine($"Service arn: {serviceARN}"); Console.WriteLine($"Service name: {serviceARN.Split("/").Last()}"); } } Console.WriteLine(new string('-', 80)); } /// <summary> /// List tasks in every cluster /// </summary> private static async Task ListTaskARNs() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"3. List Task ARNs in every cluster."); var clusterARNs = await _ecsWrapper.GetClusterARNSAsync(); foreach (var clusterARN in clusterARNs) { Console.WriteLine($"Getting tasks for cluster name: {clusterARN.Split("/").Last()}"); Console.WriteLine(new string('.', 5)); var taskARNs = await _ecsWrapper.GetTaskARNsAsync(clusterARN); foreach (var taskARN in taskARNs) { Console.WriteLine($"Task arn: {taskARN}"); } } Console.WriteLine(new string('-', 80)); } }

Métodos de wrapper que são chamados pelo cenário para gerenciar as ECS ações da Amazon.

using Amazon.ECS; using Amazon.ECS.Model; using Microsoft.Extensions.Logging; namespace ECSActions; public class ECSWrapper { private readonly AmazonECSClient _ecsClient; private readonly ILogger<ECSWrapper> _logger; /// <summary> /// Constructor for the ECS wrapper. /// </summary> /// <param name="ecsClient">The injected ECS client.</param> /// <param name="logger">The injected logger for the wrapper.</param> public ECSWrapper(AmazonECSClient ecsClient, ILogger<ECSWrapper> logger) { _logger = logger; _ecsClient = ecsClient; } /// <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}"); } } /// <summary> /// List service ARNs available. /// </summary> /// <param name="clusterARN">The arn of the ECS cluster.</param> /// <returns>The ARN list of services in given cluster.</returns> public async Task<List<string>> GetServiceARNSAsync(string clusterARN) { List<string> serviceArns = new List<string>(); var request = new ListServicesRequest { Cluster = clusterARN }; // Call the ListServices API operation and get the list of service ARNs var serviceList = _ecsClient.Paginators.ListServices(request); await foreach (var serviceARN in serviceList.ServiceArns) { if (serviceARN is null) continue; serviceArns.Add(serviceARN); } if (serviceArns.Count == 0) { _logger.LogWarning($"No services found in cluster {clusterARN} ."); } return serviceArns; } /// <summary> /// List task ARNs available. /// </summary> /// <param name="clusterARN">The arn of the ECS cluster.</param> /// <returns>The ARN list of tasks in given cluster.</returns> public async Task<List<string>> GetTaskARNsAsync(string clusterARN) { // Set up the request to describe the tasks in the service var listTasksRequest = new ListTasksRequest { Cluster = clusterARN }; List<string> taskArns = new List<string>(); // Call the ListTasks API operation and get the list of task ARNs var tasks = _ecsClient.Paginators.ListTasks(listTasksRequest); await foreach (var task in tasks.TaskArns) { if (task is null) continue; taskArns.Add(task); } if (taskArns.Count == 0) { _logger.LogWarning("No tasks found in cluster: " + clusterARN); } return taskArns; } }