Amazon ECS examples using Amazon SDK for .NET - Amazon SDK for .NET
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon ECS examples using Amazon SDK for .NET

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for .NET with Amazon ECS.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Get started

The following code example shows how to get started using Amazon ECS.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

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(); } } }
  • For API details, see ListClusters in Amazon SDK for .NET API Reference.

Actions

The following code example shows how to use ListClusters.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon 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}"); } }
  • For API details, see ListClusters in Amazon SDK for .NET API Reference.

The following code example shows how to use ListServices.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <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; }
  • For API details, see ListServices in Amazon SDK for .NET API Reference.

The following code example shows how to use ListTasks.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <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; }
  • For API details, see ListTasks in Amazon SDK for .NET API Reference.

Scenarios

The following code example shows how to:

  • Get a list of all clusters.

  • Get services for a cluster.

  • Get tasks for a cluster.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Run an interactive scenario at a command prompt.

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

Wrapper methods that are called by the scenario to manage Amazon ECS actions.

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