简体   繁体   中英

How to get AWS Fargate task instance metadata inside running container?

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/

在 AWS Fargate 中运行时,您可以使用任务元数据终端节点来检索有关任务的信息,包括其 ARN。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM