(Tags): Tagging within custom construct does not apply tag to the AWS resource
See original GitHub issueWhat 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:
- Created 2 years ago
- Comments:8 (4 by maintainers)
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: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.