aws / aws/aws-cdk

aws-cdk-lib: NestedStack reference of CustomResource with Fn results in "id.replace is not a function"

Open
#30,130 1 comment 0 reactions 0 assignees View on GitHub
@aws-cdk/custom-resources bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
74

Description

### Describe the bug

When I pass a custom resource to a nested stack via props, and then use `getAttString()` on the custom resource with an input of something like `Fn.ref(AWS::Region)`, when I call `app.synth()`, I get ` TypeError: id.replace is not a function`.

### Expected Behavior

I expect this to work as it does when the custom resource is created in the nested stack. This error does not occur if the custom resource is not passed across stacks via props.

### Current Behavior

TypeError: id.replace is not a function

9 | const stack = new BugStack(app, 'MyTestStack');
10 |
> 11 | app.synth();
| ^
12 |
13 | const template = Template.fromStack(stack);
14 | });

at sanitizeId (node_modules/constructs/src/construct.ts:552:13)
at Node.tryFindChild (node_modules/constructs/src/construct.ts:119:27)
at createNestedStackParameter (node_modules/aws-cdk-lib/core/lib/private/refs.js:1:6613)
at resolveValue (node_modules/aws-cdk-lib/core/lib/private/refs.js:1:3177)
at resolveReferences (node_modules/aws-cdk-lib/core/lib/private/refs.js:1:1516)
at prepareApp (node_modules/aws-cdk-lib/core/lib/private/prepare-app.js:1:904)
at synthesize (node_modules/aws-cdk-lib/core/lib/private/synthesis.js:1:1632)
at App.synth (node_modules/aws-cdk-lib/core/lib/stage.js:1:2365)
at Object. (test/bug.test.ts:11:9)

### Reproduction Steps

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

console.log("Creating bug stack");

let func: IFunction = new Function(this, "AvailabilityZoneMapperFunction", {
runtime: Runtime.PYTHON_3_12,
code: Code.fromInline("\ndef handler(event, context):\n return"),
handler: "index.handler",
memorySize: 512,
});

let mapper: CustomResource = new CustomResource(this, "AvailabilityZoneMapper", {
serviceToken: func.functionArn,
});

new NestedStackWithReference(this, "NestedStack", {
mapper: mapper
});
}
}

export interface NestedStackWithReferenceProps extends NestedStackProps
{
readonly mapper: CustomResource;
}

export class NestedStackWithReference extends NestedStack
{
constructor(scope: Construct, id: string, props: NestedStackWithReferenceProps)
{
super(scope, id, props);

new Queue(this, "Queue", {
queueName: props.mapper.getAttString(Fn.ref("AWS::Region"))
});
}
}
```

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.138.0 (build 6b41c8b)

### Framework Version

_No response_

### Node.js Version

v20.9.0

### OS

darwin

### Language

TypeScript

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the reproduction in test/bug.test.ts and the failing path through aws-cdk-lib/core/lib/private/refs.js, especially createNestedStackParameter and resolveValue. Run app.synth() to reproduce the TypeError; done means the nested-stack custom-resource reference with Fn.ref(AWS::Region) synthesizes successfully without the id.replace error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.