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.6k stars 3.89k forks source link

CfnInclude: Cannot add metadata to NestedStack when including template #29853

Open peermuellerxw opened 6 months ago

peermuellerxw commented 6 months ago

Describe the bug

When I try to add metadata to a stack that is created with CfnInclude I get the following error:

    at mergeObjectsWithoutDuplicates (/.../cfninclude-example/node_modules/aws-cdk-lib/core/lib/stack.js:2:854)

This seems confusing as I was trying to add a different metadata key/value pair via cdk.

Expected Behavior

I expected the templates to synthesize and the nested stack to have all metadata values

Current Behavior

No template is synthesized

Reproduction Steps

The template:

AWSTemplateFormatVersion: 2010-09-09
Metadata:
  some-data: "some-value"

Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: new-bucket

The CDK app:

#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import path = require('path');
import { CfnInclude } from 'aws-cdk-lib/cloudformation-include';

const app = new cdk.App();

class ParentStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new CfnincludeExampleStack(this, 'CfnincludeExampleStack');
  }
}

class CfnincludeExampleStack extends cdk.NestedStack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const template = new CfnInclude(this, 'CfnincludeTemplate', {
      templateFile: path.join('./include-stack.yaml'),
    });

    this.addMetadata('more-data', 'more-value');

    //This seems to work, but it's not the right way to do it
    //(Stack.of(template.stack).node.defaultChild as CfnStack).stack.addMetadata('more-data', 'more-value');
  }
}

new ParentStack(app, 'ParentStack');

Possible Solution

//This seems to work, but it's not the right way to do it
(Stack.of(template.stack).node.defaultChild as CfnStack).stack.addMetadata('more-data', 'more-value');

Additional Information/Context

This only happens with nested stacks.

CDK CLI Version

2.131.0 (build 92b912d)

Framework Version

No response

Node.js Version

v20.10.0

OS

MacOs Sonoma

Language

TypeScript

Language Version

5.3.3

Other information

No response

khushail commented 5 months ago

thanks for reporting this bug with nested stack metadata addition @peermuellerxw . I also see the same error msg as reported by you. Although I am looking for a workaround , suggested commented solution shared by you does not work for me. will share if I get any updates on other workaround.