Skip to content

aws-cdk-lib/aws-sns-subscriptions: Cannot add Lambda Subscription when Using a Lambda Construct from a SymLinked Construct Library #24788

Closed
@adamgilbert912

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

  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

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-lambdaRelated to AWS LambdabugThis issue is a bug.effort/mediumMedium work item – several days of effortp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions