aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.6k stars 3.9k forks source link

SNS Lambda Subscription Conflict #7832

Open robwettach opened 4 years ago

robwettach commented 4 years ago

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

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 commented 4 years ago

Note that the SQS Subscription does this properly.

robwettach commented 3 years ago

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.

fwang commented 2 years ago

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

JelsB commented 2 years ago

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

leantorres73 commented 2 years ago

No updates here??

PatrykMilewski commented 2 years ago

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.

enheit commented 4 months ago

I also have the same issue.

I pass topicArn to the NestedStack via props and subscribe a lambda like this.

const topic = Topic.fromTopicArn(this, "Topic", this.#props.topicArn)

topic.addSubscription(subscription)

And it says the following error

Error: A subscription with id "Topic" already exists under the scope UglylookApiStack/SpecialistLambdas/ApplySpecialistLabelUpdateToSpecialistsLambda
    at Import.addSubscription (/home/enheit/Documents/projects/uglylook-api/node_modules/aws-cdk-lib/aws-sns/lib/topic-base.js:1:1481)
    at ApplySpecialistLabelUpdateToSpecialistsLambdaConfig._ApplySpecialistLabelUpdateToSpecialistsLambdaConfig_registerEventHandler (/home/enheit/Documents/projects/uglylook-api/src/modules/core/use-cases/specialist-labels/update-specialist-label/event-handlers/apply-specialist-label-update-to-specialists/cdk/apply-specialist-label-update-to-specialists.cdk.ts:54:11)
    at new ApplySpecialistLabelUpdateToSpecialistsLambdaConfig (/home/enheit/Documents/projects/uglylook-api/src/modules/core/use-cases/specialist-labels/update-specialist-label/event-handlers/apply-specialist-label-update-to-specialists/cdk/apply-specialist-label-update-to-specialists.cdk.ts:35:31)
    at SpecialistLambdas._SpecialistLambdas_initApplySpecialistLabelUpdateToSpecialists (/home/enheit/Documents/projects/uglylook-api/lib/lambdas/admin/specialist-lambdas.nested-stack.ts:115:5)
    at new SpecialistLambdas (/home/enheit/Documents/projects/uglylook-api/lib/lambdas/admin/specialist-lambdas.nested-stack.ts:45:54)
    at new UglyicelookApiStack (/home/enheit/Documents/projects/uglylook-api/lib/uglylook-api.stack.ts:284:5)
    at Object.<anonymous> (/home/enheit/Documents/projects/uglylook-api/bin/uglylook-api.ts:11:1)
    at Module._compile (node:internal/modules/cjs/loader:1369:14)
    at Module.m._compile (/home/enheit/Documents/projects/uglylook-api/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)

Subprocess exited with error 1