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/aws-sns-subscriptions: Cannot add Lambda Subscription when Using a Lambda Construct from a SymLinked Construct Library #24788

Closed
adamgilbert912 opened this issue Mar 25, 2023 · 5 comments · Fixed by #26206
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@adamgilbert912
Copy link

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

  1. npm link a construct library locally that has a lambda function construct.
  2. Create a new lambda function from that construct library
  3. Create an SNS topic using new Topic
  4. Add a new lambda subscription to the topic using the addSubscription method and new LambdaSubscription
  5. 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

public bind(topic: sns.ITopic): sns.TopicSubscriptionConfig {
// Create subscription under *consuming* construct to make sure it ends up
// in the correct stack in cases of cross-stack subscriptions.
if (!(this.fn instanceof Construct)) {
throw new Error('The supplied lambda Function object must be an instance of Construct');
}

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

@adamgilbert912 adamgilbert912 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 25, 2023
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Mar 25, 2023
@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Mar 27, 2023
@pahud
Copy link
Contributor

pahud commented Mar 27, 2023

Thank you for pointing out this with aws/constructs@bef8e4d. I am making it p2 now and any pull request would be welcome and appreciated.

@acevesp
Copy link

acevesp commented Apr 17, 2023

Any workarounds for this?

@willjstevens
Copy link

+1. I'm getting the same.

@mjeanroy
Copy link
Contributor

mjeanroy commented Jul 3, 2023

I am making it p2 now and any pull request would be welcome and appreciated.

I just opened #26206 as I'm still suffering with this issue 🙂

@mergify mergify bot closed this as completed in #26206 Jul 3, 2023
mergify bot pushed a commit that referenced this issue Jul 3, 2023
…ambda Construct from a different Construct Library (#26206)

Fix the "is construct" check to ensure one can add a LambdaSubscription to an sns topic using the "addSubscription" method regardless of whether it came from a symlinked construct library or not.

Closes #24788.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

github-actions bot commented Jul 3, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
5 participants