文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 aARN 取得 Amazon ECS 叢集、服務和任務的 AWS SDK 資訊
以下程式碼範例顯示做法:
取得所有叢集的清單。
取得叢集的服務。
取得叢集的任務。
- .NET
-
- AWS SDK for .NET
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 在命令提示中執行互動式案例。
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)); } }
案例呼叫來管理 Amazon ECS 動作的包裝程式方法。
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; } }
-
如需 API 詳細資訊,請參閱 AWS SDK for .NET API 參考中的下列主題。
-
案例
Amazon EFS