Skip to content

Retrieving Tags for Stopped Containers in Amazon ECS

0

Hi team,

I am using Amazon ECS and would like to know if it's possible to retrieve tags for stopped containers. While I can fetch task-level tags for stopped tasks using the DescribeTasks API, I am unsure if tags associated with individual containers are accessible after they stop.

Could you clarify:

  • Whether tags for stopped containers can be retrieved?
  • If so, which API or method should be used?

Thank you for your help in advance!

asked a year ago161 views

1 Answer
0

When working with Amazon ECS, you can retrieve tags for stopped tasks, including their containers, using the task metadata endpoint or AWS CLI commands.

For retrieving tags of stopped tasks and their containers, you can use the AWS CLI with the describe-tasks command. First, list the stopped tasks in your cluster:

aws ecs list-tasks \
--cluster cluster_name \
--desired-status STOPPED \
--region region

Then, describe the specific stopped task to get detailed information:

aws ecs describe-tasks \
--cluster cluster_name \
--tasks task_ARN \
--region region

This will provide you with information about the stopped task, including the stop code, stopped reason, and container details.

For more comprehensive tag information, you can use the task metadata endpoint. The task metadata endpoint version 4 provides a specific path for retrieving task and container instance tags:

${ECS_CONTAINER_METADATA_URI_V4}/taskWithTags

This endpoint returns metadata for the task along with the task and container instance tags that can be retrieved using the ListTagsForResource API. The response includes task tags in the TaskTags field and container instance tags in the ContainerInstanceTags field.

Similarly, in task metadata endpoint version 2, you can use:

169.254.170.2/v2/metadata/taskWithTags

It's important to note that these metadata endpoints are accessible from within the container itself while it's running, so they wouldn't be directly accessible for already stopped containers. For stopped containers, the AWS CLI approach using describe-tasks is the recommended method to retrieve the available information.
Sources
Amazon ECS task metadata endpoint version 4 - Amazon Elastic Container Service
Amazon ECS task metadata v4 examples - Amazon Elastic Container Service
Amazon ECS task metadata endpoint version 2 - Amazon Elastic Container Service
Viewing Amazon ECS stopped task errors - Amazon Elastic Container Service

answered a year ago

AWS
SUPPORT ENGINEER

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.