title: How to get AWS Fargate task instance metadata inside running container?
body: <p>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.</p>
  
  <p>I have referred to the AWS documentation on <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/container-metadata.html" rel="noreferrer">Amazon ECS Container Metadata File</a></p>
  
  <p>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.</p>
  
  <p>Any way that I can get the task id (guid) from inside the running container?</p>
  
  <p><strong>Solved - Notes:</strong></p>
  
  <p>The ECS Task Endpoint provided the information needed.  From within the Docker container, you can curl (or programmatically pull) from the URL <a href="http://169.254.170.2/v2/metadata" rel="noreferrer">http://169.254.170.2/v2/metadata</a> 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.)</p>
  
  <p>Note that for Farpoint, you have to use v2 of the endpoint.</p>
  
  <p>Update 11/5/2018</p>
  
  <p>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.</p>
  
  <p>GitHub - <a href="https://github.com/tgourley/AwsContainerInspection" rel="noreferrer">https://github.com/tgourley/AwsContainerInspection</a></p>
  
  <p>Nuget - <a href="https://www.nuget.org/packages/AwsContainerInspection/" rel="noreferrer">https://www.nuget.org/packages/AwsContainerInspection/</a></p>