aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

SNS Lambda Subscription Conflict

Open robwettach opened this issue 5 years ago • 5 comments

When subscribing multiple SNS Topics to a single Lambda Function, if two of those Topics have the same id (scope-local - this could happen if those Topics are in different scopes) then the second subscription will fail to be created with:

A subscription with id "SomeId" already exists under the scope Stack/Resource

Reproduction Steps

See robwettach/sns-lambda-subscription-conflict

Error Log

A subscription with id "SomeId" already exists under the scope Stack/Resource

Environment

  • CLI Version : 1.37.0 (build e4709de)
  • Framework Version: 1.37.0
  • OS : macOS Mojave 10.14.6
  • Language : TypeScript (all)

Other

I believe changing this line from

subscriberId: topic.node.id,

to

subscriberId: topic.node.uniqueId,

will solve this issue.


This is :bug: Bug Report

robwettach avatar May 06 '20 19:05 robwettach

Note that the SQS Subscription does this properly.

robwettach avatar May 07 '20 20:05 robwettach

I just ran into this same issue again over a year later. In this case I created a sns.Topic within an enclosing construct and set its id to "Resource". This of course clashes with the default name of basically any other resource.

robwettach avatar Sep 20 '21 22:09 robwettach

One of our SST users ran into the same issue. I just submitted a PR for this.

fwang avatar Feb 18 '22 03:02 fwang

I just ran into the same problem. I also have an sns.Topic within a custom construct. @fwang, thanks for the PR!

JelsB avatar Feb 18 '22 13:02 JelsB

No updates here??

leantorres73 avatar Apr 22 '22 17:04 leantorres73

I have the same error message, but different setup. I'm importing the topic using:

const topic = sns.Topic.fromTopicArn(
  this,
  'topic-id'
  `arn-goes-here`,
);
this.addEventSource(new SnsEventSource(topic));

And then I'm getting this error message:

Error: A subscription with id "topic-id" already exists under the scope stack-name/lambda-id

My Lambda is wrapped over a custom construct, so I think it's not only a problem of wrapping the Topic in a custom construct

I also managed to workaround it, by extracting the sns.Topic.fromTopicArn( [...] ) call out of custom Lambda construct to one level up and then passing it as props.

PatrykMilewski avatar Oct 05 '22 09:10 PatrykMilewski