How to get Task ID from within ECS container?

房东的猫 提交于 2020-08-27 03:12:31

问题


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?


回答1:


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.




回答2:


We set it with the so called user data, which are executed at the start of the machine. There are multiple ways to set it, for example: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-console

It could look like this:

#!/bin/bash

cat <<'EOF' >> /etc/ecs/ecs.config
ECS_CLUSTER=ecs-staging
ECS_ENABLE_CONTAINER_METADATA=true
EOF

Important: Adjust the ECS_CLUSTER above to match your cluster name, otherwise the instance will not connect to that cluster.




回答3:


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




回答4:


From the AWS ECS cli Documentation

Command:

aws ecs list-tasks --cluster default

Output:

{
    "taskArns": [
        "arn:aws:ecs:us-east-1:<aws_account_id>:task/0cc43cdb-3bee-4407-9c26-c0e6ea5bee84",
        "arn:aws:ecs:us-east-1:<aws_account_id>:task/6b809ef6-c67e-4467-921f-ee261c15a0a1"
    ]
}

To list the tasks on a particular container instance

This example command lists the tasks of a specified container instance, using the container instance UUID as a filter.

Command:

aws ecs list-tasks --cluster default --container-instance f6bbb147-5370-4ace-8c73-c7181ded911f

Output:

{
    "taskArns": [
        "arn:aws:ecs:us-east-1:<aws_account_id>:task/0cc43cdb-3bee-4407-9c26-c0e6ea5bee84"
    ]
}


来源:https://stackoverflow.com/questions/48819809/how-to-get-task-id-from-within-ecs-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!