aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.33k stars 3.76k forks source link

(dynamodb): Dynamo TableV2 does not work with Tags.of #30631

Open ckuehne opened 5 days ago

ckuehne commented 5 days ago

Describe the bug

The aws_dynamodb.TableV2 construct does not support adding tags via Tags.of.

I.e., the following code does not create the myKey tag.

import * as cdk from 'aws-cdk-lib';
import {aws_dynamodb, Tags} from "aws-cdk-lib";

const app = new cdk.App();
const stack = new cdk.Stack(app, 'Stack');

const globalTable = new aws_dynamodb.TableV2(stack, 'GlobalTable', {
    partitionKey: {name: 'pk', type: aws_dynamodb.AttributeType.STRING},
});

Tags.of(globalTable).add('myKey', 'myValue');

Expected Behavior

Creates tag.

Current Behavior

Does not create tag.

Reproduction Steps

import * as cdk from 'aws-cdk-lib';
import {aws_dynamodb, Tags} from "aws-cdk-lib";

const app = new cdk.App();
const stack = new cdk.Stack(app, 'Stack');

const globalTable = new aws_dynamodb.TableV2(stack, 'GlobalTable', {
    partitionKey: {name: 'pk', type: aws_dynamodb.AttributeType.STRING},
});

Tags.of(globalTable).add('myKey', 'myValue');

cdk synth

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.147.0 (build 3338fc0)

Framework Version

No response

Node.js Version

v20.3.1

OS

Macos

Language

TypeScript

Language Version

TypeScript 5.4.5

Other information

No response

TirushV commented 5 days ago

Suggestion: Try using aspects to add tags to the current stack where Aspect is called.

ckuehne commented 5 days ago

Suggestion: Try using aspects to add tags to the current stack where Aspect is called.

Yes, this would work. But still the buggy behaviour is unexpected and should be fixed.

TirushV commented 5 days ago

Thats true. I agree with you.

I had this use case before and solved it by getting the all event notification first inside a list and then recreating then along with new one again. (Temporary solution) XD

khushail commented 3 days ago

Hey @ckuehne , thanks for reporting this. I reproduced this issue and agree that this is a buggy behavior. Hence marking it as P3.

However if you try assigning the tags like this, this adds the tags to the table. Usage of ASPECTS (sample code) would also be another workaround


    const globalTable = new aws_dynamodb.TableV2(this, 'GlobalTable', {
      partitionKey: {name: 'pk', type: aws_dynamodb.AttributeType.STRING},
      tags: [{key: "myKeyTag", value: "myValueTag"}]
    });