Unbreakable
Unbreakable

Reputation: 8132

Is it possible to access task definition from inside a docker container

I want to be able to access some value which is baked in task.json of ECS from inside a container. Is it possible? I know we can add environment section in task definition and which can be referenced in docker container, but can I access other entities too, for instance suppose I want to access awslogs-group from within the container. How to do it?

{
  "family": "task-poc",
  "containerDefinitions": [
    {
      "image": "ABC 123",
      "name": "logging-poc-1",
      "cpu": 1024,
      "memory": 1024,
      "essential": true,
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "my_log_group",
          "awslogs-region": "us-east-1",
          "awslogs-stream-prefix": "foo-transactions-stg-secops"
        }
      }
    }
  ]
}

For environment variable I can just do something like

In C# - Environment.GetEnvironmentVariable("MyKey");

Upvotes: 0

Views: 1098

Answers (1)

Adiii
Adiii

Reputation: 60124

No you can not get the log group as simple as you get Environment variable but you can get these metrics below.

curl $ECS_CONTAINER_METADATA_URI

you do this call from your application to get container meta data

or

For Linux instances:

cat $ECS_CONTAINER_METADATA_FILE

For Windows instances (PowerShell):

Get-Content -path $env:ECS_CONTAINER_METADATA_FILE

This will return

The following example shows a container metadata file in the READY status.

{
    "Cluster": "default",
    "ContainerInstanceARN": "arn:aws:ecs:us-west-2:012345678910:container-instance/1f73d099-b914-411c-a9ff-81633b7741dd",
    "TaskARN": "arn:aws:ecs:us-west-2:012345678910:task/2b88376d-aba3-4950-9ddf-bcb0f388a40c",
    "ContainerID": "98e44444008169587b826b4cd76c6732e5899747e753af1e19a35db64f9e9c32",
    "ContainerName": "metadata",
    "DockerContainerName": "/ecs-metadata-7-metadata-f0edfbd6d09fdef20800",
    "ImageID": "sha256:c24f66af34b4d76558f7743109e2476b6325fcf6cc167c6e1e07cd121a22b341",
    "ImageName": "httpd:2.4",
    "PortMappings": [
        {
            "ContainerPort": 80,
            "HostPort": 80,
            "BindIp": "",
            "Protocol": "tcp"
        }
    ],
    "Networks": [
        {
            "NetworkMode": "bridge",
            "IPv4Addresses": [
                "172.17.0.2"
            ]
        }
    ],
    "MetadataFileStatus": "READY"
}

Shortest way:

Pass al the value fo log config to ENV.

        "options": {
          "awslogs-group": "my_log_group",
          "awslogs-region": "us-east-1",
          "awslogs-stream-prefix": "foo-transactions-stg-secops"
        }

pass the above to ENV

          "environment": [
                {
                    "name": "awslogs-group",
                    "value": "my_log_group"
                },
                {
                    "name": "awslogs-region",
                    "value": "us-east-1"
                },
                {
                    "name": "awslogs-stream-prefix",
                    "value": "foo-transactions-stg-secops"
                }
            ],

Then get as a ENV in c#

n C# - Environment.GetEnvironmentVariable("awslogs-group");

Long way:

  • Get the TaskARN from Container Metadata d
  • Use the TaskArn in your application to describe the task
  • From describe Task get the task definition ARN
  • Describe task definition and you will get the log group

Upvotes: 2

Related Questions