9

When a task launches inside of AWS Fargate, it has a task id (guid) that is used for Cloudwatch logs and can be used as a unique "run id". I am launching a .NET core application into a AWS container and would like to find a programmatic way to pull the task id.

I have referred to the AWS documentation on Amazon ECS Container Metadata File

But it does not appear that the agent is turned on for Fargate, as the ECS_CONTAINER_METADATA_FILE environment variable does not appear to be available.

Any way that I can get the task id (guid) from inside the running container?

Solved - Notes:

The ECS Task Endpoint provided the information needed. From within the Docker container, you can curl (or programmatically pull) from the URL http://169.254.170.2/v2/metadata and it will provide you with the task metadata of the container making the call. I was able to test this on two separate running containers and got back their specific task ARN values (unique GUID for each container.)

Note that for Farpoint, you have to use v2 of the endpoint.

Update 11/5/2018

I have created a .NET Standard Library called AwsContainerInspection that facilitates the parsing of the metadata from the AWS ECS Task Metadata Endpoint and returns a class object. I use it in my code to get the task GUID for logging and other things.

GitHub - https://github.com/tgourley/AwsContainerInspection

Nuget - https://www.nuget.org/packages/AwsContainerInspection/

Trey Gourley
  • 391
  • 1
  • 7
  • 19
  • 1
    Thank you for posting this code. That was exactly what I am looking for one and half day. – Shwe Mar 08 '19 at 15:13
  • Does anyone know if pulling this metadata in is implemented in the Java SDK (or any other SDK?) – J. Dimeo Feb 02 '21 at 19:32

1 Answers1

9

When running in AWS Fargate, you can use the task metadata endpoint to retrieve information about the task, including its ARN.

Samuel Karp
  • 4,373
  • 22
  • 34
  • 1
    I have two web containers (load balanced through the same service) that get returned by http://169.254.170.2/v2/metadata How can I know which one matches the container making the call? – Trey Gourley Nov 02 '18 at 06:10
  • Actually on closer inspection, it appears that the same container is represented twice in the JSON data returned. One with a "KnownStatus" of "RESOURCES_PROVISIONED" and another with a status of "RUNNING". It appears that this service will give me what I want. Thank you! – Trey Gourley Nov 02 '18 at 13:15