question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(Tags): Tagging within custom construct does not apply tag to the AWS resource

See original GitHub issue

What is the problem?

When inside a custom construct I call Tags.of(asg).add("Name", "my-asg-name");, the resource (asg in this case) does not get the tag set in AWS during a deploy. The resources are created properly, and inherited tags are set, but not tags added using the Tags.of(...).add(...) method inside the construct.

Reproduction Steps

create custom construct:

import { Construct } from "constructs";
import { Cluster } from "aws-cdk-lib/aws-ecs";
import { Tags } from "aws-cdk-lib";
import { Vpc } from "aws-cdk-lib/aws-ec2";

export class MyEcsCluster extends Construct {
    constructor(scope: Construct, id: string, vpc: Vpc) {
        super(scope, id);
        const cluster = new Cluster(this, "my-ecs-cluster", { vpc: vpc });
        Tags.of(cluster).add("Name", "my-ecs-cluster");
    }
}

Create a stack that instantiates the construct:

import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import { Vpc } from "aws-cdk-lib/aws-ec2";
import { MyEcsCluster } from "my-constructs";
export class TestStack extends Stack { constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);
    // Import existing VPC
    const vpc: Vpc = Vpc.fromLookup(this, "primary", {
      vpcName: "primary",
    }) as Vpc;

    new MyEcsCluster(this, "test-custom-construct", vpc);

  }
}

What did you expect to happen?

When you run cdk deploy on this stack it should create an ECS cluster with a tag key of “Name” and value of “my-ecs-cluster”

What actually happened?

It created the ECS cluster, but did not add the tag

CDK CLI Version

2.16.0 (build 4c77925)

Framework Version

aws-cdk-lib: 2.16.0, constructs: 10.0.87

Node.js Version

v14.18.0

OS

macOS Catalina v10.15.7

Language

Typescript

Language Version

TypeScript 4.6.2

Other information

Current workaround is to use escape hatch to directly add tags within the construct:

const cfnCluster = cluster.node.defaultChild as CfnCluster;
 cfnCluster.addPropertyOverride('Tags', [{
     Key: 'Name',
     Value: 'my-ecs-cluster',
 },]);

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sdommecommented, Mar 18, 2022

Stumbled upon this issue during investigating an issue I have with subnets tags you need to use the albController subnet auto discovery. We have a custom construct wrapped around the aws-cdk-lib/aws-eks/cluster construct. The method we relying on is https://github.com/aws/aws-cdk/blob/90bc19768edbde6ae8f1a759d7c4e7a0764b15c5/packages/%40aws-cdk/aws-eks/lib/cluster.ts#L1330 If I locally develop the library and have to npm link it to my stack repo, the tagging usually happened within the lib doesn’t work at all. Not even if I use the Tags.of function. To test I build the npm package and used it in a normal way in my stack. cdk diff showed me all the missing tags. Working with:

  • Node 16
  • CDK 2.17
0reactions
github-actions[bot]commented, May 26, 2022

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tagging - AWS Cloud Development Kit (AWS CDK) v2
Tags are informational key-value elements that you can add to constructs in your AWS CDK app. A tag applied to a given construct...
Read more >
Tagging AWS resources - AWS General Reference
You can assign metadata to your AWS resources in the form of tags. Each tag is a label consisting of a user-defined key...
Read more >
Tag your Amazon EC2 resources - AWS Documentation
This ensures that resources are either created with tags or not created at all, and that no resources are left untagged at any...
Read more >
Tagging your AWS resources with Tag Editor
Learn about tags and how tagging your AWS resources can help you organize, manage, and secure them.
Read more >
Implement AWS resource tagging strategy using AWS Tag ...
When a tag policy is applied to your AWS account, users are unable to create resources using noncompliant tags. You can enforce specific...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found