Skip to content

(ecs): ContainerDefinition.addDockerLabel #29728

@blimmer

Description

@blimmer

Describe the feature

It would be nice to be able to add docker labels after the container has been constructed.

For example, I can call addEnvironment to add an environment variable after the fact: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.ContainerDefinition.html#addwbrenvironmentname-value

However, I can't do the same with docker labels.

Use Case

I have a construct that automatically applies Datadog monitoring information to a TaskDefinition with an application container. Datadog requires that I set some information as Docker Labels for observability:

Screenshot 2024-04-04 at 12 23 53

However, I want to add these labels after the user already created their default container.

Proposed Solution

Add a .addDockerLabel method similar to .addEnvironment to the ContainerDefiniton construct. It'd be similar to #2559 / #17889

Other Information

You can hack this like so:

const defaultContainerAsAny = defaultContainer as any;
defaultContainerAsAny.props.dockerLabels = Object.assign(defaultContainerAsAny.props.dockerLabels || {}, {
  "com.datadoghq.tags.env": this.env,
  "com.datadoghq.tags.service": serviceName,

  // TODO: The ECS CircleCI orb doesn't expose the ability to set `dockerLabels` when deploying
  // a task definition update. See https://github.com/CircleCI-Public/aws-ecs-orb/issues/119
  // "com.datadoghq.tags.version": "",

  "com.datadoghq.ad.instances": JSON.stringify([
    { host: "%%host%%", port: defaultContainer.portMappings.length ? defaultContainer.ingressPort : null },
  ]),
  "com.datadoghq.ad.check_names": JSON.stringify([serviceName]),
  "com.datadoghq.ad.init_configs": JSON.stringify([{}]),
});

But it's not ideal

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.135.0

Environment details (OS name and version, etc.)

MacOS Sonoma

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-ecsRelated to Amazon Elastic Containereffort/smallSmall work item – less than a day of effortfeature-requestA feature should be added or improved.good first issueRelated to contributions. See CONTRIBUTING.mdp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions