View a markdown version of this page

Hello Amazon ECS - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Hello Amazon ECS

The following code examples show how to get started using Amazon ECS.

.NET
SDK for .NET (v4)
Note

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

using Amazon.ECS; using Amazon.ECS.Model; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Microsoft.Extensions.Logging.Debug; namespace ECSActions; /// <summary> /// A class that introduces the Amazon ECS Client by listing the /// cluster ARNs for the account. /// </summary> 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 client. // Use your AWS profile name, or leave it blank to use the default profile. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddAWSService<IAmazonECS>() ) .Build(); var amazonECSClient = host.Services.GetRequiredService<IAmazonECS>(); Console.WriteLine($"Hello Amazon ECS! Following are some cluster ARNS available in the your account"); Console.WriteLine(); var clusters = new List<string>(); var clustersPaginator = amazonECSClient.Paginators.ListClusters(new ListClustersRequest()); await foreach (var response in clustersPaginator.Responses) { clusters.AddRange(response.ClusterArns); } if (clusters.Count > 0) { clusters.ForEach(cluster => { Console.WriteLine($"\tARN: {cluster}"); Console.WriteLine($"Cluster Name: {cluster.Split("/").Last()}"); Console.WriteLine(); }); } else { Console.WriteLine("No clusters were found."); } } }
  • For API details, see ListClusters in AWS SDK for .NET API Reference.

Python
SDK for Python (Boto3)
Note

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

import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def hello_ecs(): """ Lists the Amazon ECS clusters in the current AWS region. Uses a paginator to handle large numbers of clusters. """ ecs_client = boto3.client("ecs") try: paginator = ecs_client.get_paginator("list_clusters") cluster_arns = list() for page in paginator.paginate(): cluster_arns.extend(page.get("clusterArns", list())) if cluster_arns: print(f"Found {len(cluster_arns)} ECS cluster(s):") for arn in cluster_arns: print(f" - {arn}") else: print("No ECS clusters found in the current region.") except ClientError as err: logger.error( "Error listing ECS clusters: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise if __name__ == "__main__": hello_ecs()
  • For API details, see ListClusters in AWS SDK for Python (Boto3) API Reference.