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/
When running in AWS Fargate, you can use the task metadata endpoint to retrieve information about the task, including its ARN.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With