Search code examples
amazon-web-servicesaws-cliamazon-ecsaws-fargate

Can I access ECS Fargate Task tags after the Task has exited or stopped running?


I want access to the tags of an ECS Task after it has stopped and the container has exited.

I launch a task using aws ecs run-task and attach a tag to it. I'm able to do this using the --tags option in this function, but I can only access the tags until the task completes. Once the task finishes and the container exits, I can't access the tags anymore. Is there a way to get tags of resources that are NOT currently running? This is the aws-cli command I'm using to launch a task with tags:

aws ecs run-task \
    --cluster ${CLUSTER}\
    --task-definition ${TASK_NAME}-${TASK_ENV} \
    --launch-type FARGATE \
    --network-configuration  "${AWS_VPC_CONFIGURATION}" \
    --tags key='testKey',value='1'\
    --enable-ecs-managed-tags \
;

I have tried using aws ecs list-tags-for-resource --resource-arn ${ARN} but it only shows tags if the task is still running. If I try this on a task that has already completed/exited with exitcode 0, i get this error

An error occurred (InvalidParameterException) when calling the ListTagsForResource operation: The specified task is stopped. Specify a running task and try again.

I also tried aws ecs describe-tasks but that also returns an empty array once the task has exited - "tags": [], and no actual tag values, even though the task was launched with tags.

NOTE: in my use case Task Definitions DO NOT have tags, i'm assigning a tag when the run task command is executed.


Solution

  • Even running ECS on EC2, the tasks are volatile. They don't hang around for long after failure.

    You can see info, like return codes and such, on them for maybe a few hours at best after stop.

    On Fargate, they seem to get harvested even more aggressively, so if you are trying to collect metrics or something, it's probably not a good idea to try and rely on harvesting info from stopped tasks. Rather store the info somewhere else more permanent before exiting and retrieve as needed.