Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Task ID from within ECS container?

Hello I am interested in retrieving the Task ID from within inside a running container which lives inside of a EC2 host machine.

AWS ECS documentation states there is an environment variable ECS_CONTAINER_METADATA_FILE with the location of this data but will only be set/available if ECS_ENABLE_CONTAINER_METADATA variable is set to true upon cluster/EC2 instance creation. I don't see where this can be done in the aws console.

Also, the docs state that this can be done by setting this to true inside the host machine but would require to restart the docker agent.

Is there any other way to do this without having to go inside the EC2 to set this and restart the docker agent?

like image 960
code Avatar asked Feb 16 '18 03:02

code


People also ask

How do I find my Arn ECS task?

There isn't any specific AWS command which can fetch the task ARN from the container runtime id. But this can be achieved using list-tasks and describe-tasks command of aws ecs, if you know the cluster and service name in prior.

How do I get inside ECS container?

To connect to your container instanceOpen the Amazon ECS console at https://console.aws.amazon.com/ecs/ . Select the cluster that hosts your container instance. On the Cluster page, choose ECS Instances. On the Container Instance column, select the container instance to connect to.

How can I get ECS task logs?

You can view and search these logs in the console. Open the Amazon ECS console at https://console.aws.amazon.com/ecs/ . On the Clusters page, select the cluster that contains the task to view. On the Cluster: cluster_name page, choose Tasks and select the task to view.


3 Answers

The technique I'd use is to set the environment variable in the container definition.

If you're managing your tasks via Cloudformation, the relevant yaml looks like so:

  Taskdef:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ...
      ContainerDefinitions:
        - Name: some-name
          ...
          Environment:
            - Name: AWS_DEFAULT_REGION
              Value: !Ref AWS::Region
            - Name: ECS_ENABLE_CONTAINER_METADATA
              Value: 'true'

This technique helps you keep everything straightforward and reproducible.

If you need metadata programmatically and don't have access to the metadata file, you can query the agent's metadata endpoint:

curl http://localhost:51678/v1/metadata

Note that if you're getting this information as a running task, you may not be able to connect to the loopback device, but you can connect to the EC2 instance's own IP address.

like image 190
Ben Avatar answered Oct 10 '22 10:10

Ben


This doesn't work for newer Amazon ECS container versions anymore, and in fact it's now much simpler and also enabled by default. Please refer to this docu, but here's a TL;DR:

If you're using Amazon ECS container agent version 1.39.0 and higher, you can just do this inside the docker container:

curl -s "$ECS_CONTAINER_METADATA_URI_V4/task" \
  | jq -r ".TaskARN" \
  | cut -d "/" -f 3

Here's a list of container agent releases, but if you're using :latest – you're definitely fine.

like image 45
Innokenty Avatar answered Oct 10 '22 11:10

Innokenty


Previous answers are correct, here is another way of doing this:

From the ec2 instance where container is running, run this command

curl http://localhost:51678/v1/tasks | python -mjson.tool |less

enter image description here

enter image description here

like image 5
grepit Avatar answered Oct 10 '22 11:10

grepit