Derrops
Derrops

Reputation: 8137

How can I get the TaskId of a Fargate ecs Container

Similar to this question How to get Task ID from within ECS container? but I want to get the TaskId for my Fargate task. How can you do this? Like others I want this for logging information.

I'm running a Spring App with ELK stack for logging and would like if possible to include the TaskId in the logs if possible.

Edit I actually never got this to work by the way, here is my code:

    private String getTaskIdInternal() {

        String url = System.getenv("ECS_CONTAINER_METADATA_URI_V4") + "/task";

        logger.info("Getting ecsMetaDataURL={}", url);

        if (url == null) {
            throw new RuntimeException("ECS_CONTAINER_METADATA_URI_V4 env variable not defined");
        }

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<JsonNode> response = restTemplate.getForEntity(url, JsonNode.class);

        logger.info("ecsMetaData={}", response);

        JsonNode map = response.getBody();

        String taskArn = map.get("TaskARN").asText();
        String[] splitTaskArn = taskArn.split("/");
        String taskId =  splitTaskArn[splitTaskArn.length - 1];
        logger.info("ecsTaskId={}", taskId);
        return taskId;
    }

But I always get this stack trace:

Could not get the taskId from ECS. exception=org.springframework.web.client.HttpClientErrorException: 403 Forbidden
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:118)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:103)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:732)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:690)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:646)
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:325)

Upvotes: 9

Views: 12207

Answers (3)

Manish kc
Manish kc

Reputation: 1

This will do the trick:

aws ecs list-tasks --cluster $CLUSTER_NAME --service $SERVICE_NAME --query "taskArns" --output text

Bash script will make you easier as you don't have to define the defined variables everytime.

#!/bin/bash

export AWS_PROFILE=$AWS_PROFILE
CLUSTER_NAME=$CLUSTER_NAME
SERVICE_NAME=$SERVICE_NAME
CONTAINER_NAME=$CONTAINER_NAME

# Get a list of task ARNs
TASK_ARNS=($(aws ecs list-tasks --cluster $CLUSTER_NAME --service $SERVICE_NAME --query "taskArns" --output text))

# Check if there are tasks running
if [ ${#TASK_ARNS[@]} -eq 0 ]; then
  echo "No tasks running."
  exit 1
fi

# Assume the first task in the list, you may modify this based on your logic
TASK_ID=${TASK_ARNS[0]}

# Execute command on the selected task
aws ecs execute-command --cluster $CLUSTER_NAME \
    --task $TASK_ID \
    --container $CONTAINER_NAME \
    --interactive \
    --command "/bin/sh"

Upvotes: 0

lznt
lznt

Reputation: 2586

The link in the accepted answer is for EC2 launch type. The direct doc link for Fargate is: https://docs.aws.amazon.com/AmazonECS/latest/userguide/task-metadata-endpoint-v4-fargate.html. The json content seems to be pretty much the same though.

Upvotes: 0

Chris Williams
Chris Williams

Reputation: 35258

If you're trying to get the task id in Fargate for ECS you make use of metadata endpoints.

Assuming you're using version 1.4.0 of Fargate you can get this via a http request to ${ECS_CONTAINER_METADATA_URI_V4}/task.

An example response from this endpoint is below

{
    "Cluster": "arn:aws:ecs:us-west-2:&ExampleAWSAccountNo1;:cluster/default",
    "TaskARN": "arn:aws:ecs:us-west-2:&ExampleAWSAccountNo1;:task/default/febee046097849aba589d4435207c04a",
    "Family": "query-metadata",
    "Revision": "7",
    "DesiredStatus": "RUNNING",
    "KnownStatus": "RUNNING",
    "Limits": {
        "CPU": 0.25,
        "Memory": 512
    },
    "PullStartedAt": "2020-03-26T22:25:40.420726088Z",
    "PullStoppedAt": "2020-03-26T22:26:22.235177616Z",
    "AvailabilityZone": "us-west-2c",
    "Containers": [
        {
            "DockerId": "febee046097849aba589d4435207c04aquery-metadata",
            "Name": "query-metadata",
            "DockerName": "query-metadata",
            "Image": "mreferre/eksutils",
            "ImageID": "sha256:1b146e73f801617610dcb00441c6423e7c85a7583dd4a65ed1be03cb0e123311",
            "Labels": {
                "com.amazonaws.ecs.cluster": "arn:aws:ecs:us-west-2:&ExampleAWSAccountNo1;:cluster/default",
                "com.amazonaws.ecs.container-name": "query-metadata",
                "com.amazonaws.ecs.task-arn": "arn:aws:ecs:us-west-2:&ExampleAWSAccountNo1;:task/default/febee046097849aba589d4435207c04a",
                "com.amazonaws.ecs.task-definition-family": "query-metadata",
                "com.amazonaws.ecs.task-definition-version": "7"
            },
            "DesiredStatus": "RUNNING",
            "KnownStatus": "RUNNING",
            "Limits": {
                "CPU": 2
            },
            "CreatedAt": "2020-03-26T22:26:24.534553758Z",
            "StartedAt": "2020-03-26T22:26:24.534553758Z",
            "Type": "NORMAL",
            "Networks": [
                {
                    "NetworkMode": "awsvpc",
                    "IPv4Addresses": [
                        "10.0.0.108"
                    ],
                    "AttachmentIndex": 0,
                    "IPv4SubnetCIDRBlock": "10.0.0.0/24",
                    "MACAddress": "0a:62:17:7a:36:68",
                    "DomainNameServers": [
                        "10.0.0.2"
                    ],
                    "DomainNameSearchList": [
                        "us-west-2.compute.internal"
                    ],
                    "PrivateDNSName": "ip-10-0-0-108.us-west-2.compute.internal",
                    "SubnetGatewayIpv4Address": ""
                }
            ]
        }
    ]
}

As you can see you would need to parse the TaskARN to get the TaskID (it is the last part of the ARN if you split by "/".

Amazon do specify the following in the documentation that should be noted.

For tasks using the Fargate launch type and platform versions prior to 1.4.0, the task metadata version 3 and 2 endpoint are supported. For more information, see Task Metadata Endpoint version 3 or Task Metadata Endpoint version 2.

Upvotes: 10

Related Questions