There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.
Code examples for Amazon ECS using AWS SDKs
The following code examples show you how to use Amazon Elastic Container Service with an AWS software development kit (SDK).
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.
Scenarios are code examples that show you how to accomplish specific tasks by calling multiple functions within a service or combined with other AWS services.
Get started
The following code example shows how to get started using Amazon ECS.
- .NET
-
- SDK for .NET (v4)
-
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.");
}
}
}