Contoh Amazon ECS menggunakan AWS SDK for .NET - AWS Contoh Kode SDK

Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc. GitHub

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Contoh Amazon ECS menggunakan AWS SDK for .NET

Contoh kode berikut menunjukkan kepada Anda cara melakukan tindakan dan menerapkan skenario umum AWS SDK for .NET dengan menggunakan Amazon ECS.

Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Meskipun tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks pada skenario terkait dan contoh lintas layanan.

Skenario adalah contoh kode yang menunjukkan cara menyelesaikan tugas tertentu dengan memanggil beberapa fungsi dalam layanan yang sama.

Setiap contoh menyertakan tautan ke GitHub, di mana Anda dapat menemukan petunjuk tentang cara mengatur dan menjalankan kode dalam konteks.

Memulai

Contoh kode berikut menunjukkan cara memulai menggunakan Amazon ECS.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

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(); } } }
  • Untuk detail API, lihat ListClustersdi Referensi AWS SDK for .NET API.

Tindakan

Contoh kode berikut menunjukkan cara menggunakanListClusters.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

/// <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}"); } }
  • Untuk detail API, lihat ListClustersdi Referensi AWS SDK for .NET API.

Contoh kode berikut menunjukkan cara menggunakanListServices.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

/// <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; }
  • Untuk detail API, lihat ListServicesdi Referensi AWS SDK for .NET API.

Contoh kode berikut menunjukkan cara menggunakanListTasks.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

/// <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; }
  • Untuk detail API, lihat ListTasksdi Referensi AWS SDK for .NET API.

Skenario

Contoh kode berikut ini menunjukkan cara:

  • Dapatkan daftar semua cluster.

  • Dapatkan layanan untuk cluster.

  • Dapatkan tugas untuk cluster.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankan di Repositori Contoh AWS Kode.

Jalankan skenario interaktif di penggugah/prompt perintah.

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)); } }

Metode pembungkus yang dipanggil oleh skenario untuk mengelola tindakan 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; } }