Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws-cdk-lib: incorrect rendering of CfnParameter in Fn.sub #28385

Open
zhan9san opened this issue Dec 15, 2023 · 2 comments
Open

aws-cdk-lib: incorrect rendering of CfnParameter in Fn.sub #28385

zhan9san opened this issue Dec 15, 2023 · 2 comments
Labels
@aws-cdk/core Related to core CDK functionality bug This issue is a bug. effort/medium Medium work item – several days of effort p3

Comments

@zhan9san
Copy link

zhan9san commented Dec 15, 2023

Describe the bug

The CloudFormation template generated by cdk synth is invalid if CfnParameter is in Fn.sub.

Similar to #14047 and #14068

Expected Behavior

The cdk code is valid after cdk synth runs

Current Behavior

    "Tags": [
     {
      "Key": "Environment",
      "Value": {
       "Fn::Sub": {
        "Fn::Join": [
         "",
         [
          "${AWS::StackName}-",
          {
           "Ref": "Environment"
          },
          "-foo"
         ]
        ]
       }
      }
     }
    ]
$ cfn-lint cdk.out/HelloWorldStack.template.json
E1019 Sub should be a string or array of 2 items for Resources/helloworldfunctionServiceRole6902C513/Properties/Tags/0/Value/Fn::Sub
cdk.out/HelloWorldStack.template.json:46:8

Reproduction Steps

    const env = new CfnParameter(this, 'Environment').valueAsString;
    Tags.of(this).add('Environment', Fn.sub('${AWS::StackName}-' + env + '-foo'))
$ cdk synth
$ cfn-lint cdk.out/HelloWorldStack.template.json
E1019 Sub should be a string or array of 2 items for Resources/helloworldfunctionServiceRole6902C513/Properties/Tags/0/Value/Fn::Sub
cdk.out/HelloWorldStack.template.json:46:8

Possible Solution

No response

Additional Information/Context

There is no issue if it is in the following format.

    const env = new CfnParameter(this, 'Environment').valueAsString;
    Tags.of(this).add('Environment', Fn.sub('${AWS::StackName}-' + '${env}' + '-foo', {env: env}))
    "Tags": [
     {
      "Key": "Environment",
      "Value": {
       "Fn::Sub": [
        "${AWS::StackName}-${env}-foo",
        {
         "env": {
          "Ref": "Environment"
         }
        }
       ]
      }
     }
    ]

CDK CLI Version

2.113.0 (build ccd534a)

Framework Version

No response

Node.js Version

v20.0.0

OS

MacOS

Language

TypeScript

Language Version

No response

Other information

No response

@zhan9san zhan9san added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 15, 2023
@github-actions github-actions bot added the aws-cdk-lib Related to the aws-cdk-lib package label Dec 15, 2023
@pahud
Copy link
Contributor

pahud commented Dec 15, 2023

I would write this way instead

Tags.of(this).add('Environment', `${Stack.of(this).stackName}-${env}-foo`);

Does it work for you?

@pahud pahud added p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Dec 15, 2023
@zhan9san
Copy link
Author

I would write this way instead

Tags.of(this).add('Environment', `${Stack.of(this).stackName}-${env}-foo`);

Does it work for you?

hi @pahud

Thanks for your suggestion.

I wonder what it is the recommended way to use fn.sub with cfnparameter.

If the way i do it is not correct, it would be great if it can be noted in the official document.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Dec 16, 2023
@pahud pahud added p3 and removed p2 labels Jun 11, 2024
@moelasmar moelasmar added @aws-cdk/core Related to core CDK functionality and removed aws-cdk-lib Related to the aws-cdk-lib package labels Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/core Related to core CDK functionality bug This issue is a bug. effort/medium Medium work item – several days of effort p3
Projects
None yet
Development

No branches or pull requests

3 participants