aws-cdk-lib/aws-sns-subscriptions: Cannot add Lambda Subscription when Using a Lambda Construct from a SymLinked Construct Library #24788
Closed
Description
Describe the bug
Users can't add a LambdaSubscription to an sns topic using the "addSubscription" method when the created lambda function came from a symlinked construct library.
Expected Behavior
Users can add a LambdaSubscription to an sns topic using the "addSubscription" method regardless of whether it came from a symlinked construct library or not.
Current Behavior
Stack trace:
Error: The supplied lambda Function object must be an instance of Construct
at LambdaSubscription.bind (/Users/user/Desktop/dir/project/cdk/node_modules/aws-cdk-lib/aws-sns-subscriptions/lib/lambda.js:1:968)
at Topic.addSubscription (/Users/user/Desktop/dir/project/cdk/node_modules/aws-cdk-lib/aws-sns/lib/topic-base.js:1:1018)
at new NamedStack (/Users/user/Desktop/dir/project/cdk/lib/stack-name.ts:106:33)
at Object.<anonymous> (/Users/user/Desktop/dir/project/cdk/bin/stack-name.ts:41:29)
at Module._compile (node:internal/modules/cjs/loader:1155:14)
at Module.m._compile (/Users/user/Desktop/dir/project/cdk/node_modules/ts-node/src/index.ts:1618:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
at Object.require.extensions.<computed> [as .ts] (/Users/user/Desktop/dir/project/cdk/node_modules/ts-node/src/index.ts:1621:12)
at Module.load (node:internal/modules/cjs/loader:1033:32)
at Function.Module._load (node:internal/modules/cjs/loader:868:12)
Reproduction Steps
npm link
a construct library locally that has a lambda function construct.- Create a new lambda function from that construct library
- Create an SNS topic using
new Topic
- Add a new lambda subscription to the topic using the
addSubscription
method andnew LambdaSubscription
- run
cdk synth
Basic code:
const lambda = new LambdaFromSymLinkedLibrary(this, 'Lambda');
const topic = new Topic(this, 'Topic');
topic.addSubscription(new LambdaSubscription(lambda)); //Error: The supplied lambda Function object must be an instance of Construct
Possible Solution
aws-cdk/packages/@aws-cdk/aws-sns-subscriptions/lib/lambda.ts
Lines 24 to 29 in f3fd183
This guard clause should be:
if (!Construct.isConstruct(this.fn)) {
throw new Error('The supplied lambda Function object must be an instance of Construct');
}
You can see the commit that mentions this issue in the 'constructs' repository here: aws/constructs@bef8e4d
Additional Information/Context
No response
CDK CLI Version
2.70.0
Framework Version
No response
Node.js Version
16
OS
macOS Monterey Version 12.2
Language
Typescript
Language Version
4.9.4
Other information
No response