aws / aws/aws-cdk

CfnInclude: Cannot add metadata to NestedStack when including template

Open
#29,853 1 comment 0 reactions 0 assignees View on GitHub
@aws-cdk/cloudformation-include bug effort/small p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

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

```Error: section 'Metadata' already contains 'some-data'
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_

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure with the included template and TypeScript CDK app, then inspect the mergeObjectsWithoutDuplicates path shown in core/lib/stack.js and the CfnInclude nested-stack handling. Done means synthesis succeeds and the nested stack template contains both the existing some-data metadata and the added more-data entry.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.