Use ListTasks with an AWS SDK or CLI - AWS SDK Code Examples

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

Use ListTasks with an AWS SDK or CLI

The following code examples show how to use ListTasks.

Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:

.NET
AWS SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS 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 AWS SDK for .NET API Reference.

CLI
AWS CLI

Example 1: To list the tasks in a cluster

The following list-tasks example lists all of the tasks in a cluster.

aws ecs list-tasks --cluster default

Output:

{ "taskArns": [ "arn:aws:ecs:us-west-2:123456789012:task/a1b2c3d4-5678-90ab-cdef-11111EXAMPLE", "arn:aws:ecs:us-west-2:123456789012:task/a1b2c3d4-5678-90ab-cdef-22222EXAMPLE" ] }

Example 2: To list the tasks on a particular container instance

The following list-tasks example lists the tasks on a container instance, using the container instance UUID as a filter.

aws ecs list-tasks --cluster default --container-instance a1b2c3d4-5678-90ab-cdef-33333EXAMPLE

Output:

{ "taskArns": [ "arn:aws:ecs:us-west-2:123456789012:task/a1b2c3d4-5678-90ab-cdef-44444EXAMPLE" ] }

For more information, see Amazon ECS Task Definitions in the Amazon ECS Developer Guide.

  • For API details, see ListTasks in AWS CLI Command Reference.