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/aws-sns-subscriptions): Nested stack cannot depend on a parent stack error when trying to add SNS subscription #18829

Closed
eoromb opened this issue Feb 4, 2022 · 7 comments
Labels
@aws-cdk/aws-sns-subscriptions bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/small Small work item – less than a day of effort p2

Comments

@eoromb
Copy link

eoromb commented Feb 4, 2022

What is the problem?

I got the error that nested stack cannot depend on a parent stack when trying to add SNS subscription to topic from parent stack in nested one.

Reproduction Steps

Parent stack:

  const topic = new Topic(this, 'topic', ...);
  this.nestedStack = new SomeNestedStack(this,...);

Some Nested stack:

constructor(scope: ParentStack, ...) {
   ...;  
  const queue = new Queue(...);  
  scope.topic.addSubscription(new SqsSubscription(queue, ...  ));  
}

What did you expect to happen?

Probably should not get this error.

What actually happened?

Got the following error:
Error: Nested stack 'nestedStack' cannot depend on a parent stack 'parentStack': undefined
at Object.addDependency (/builds/xymatic/bouncr/infra/node_modules/@aws-cdk/core/lib/deps.ts:73:11)
at stackName.addDependency (/builds/xymatic/bouncr/infra/node_modules/@aws-cdk/core/lib/stack.ts:271:5)
at SqsSubscription.bind (/builds/xymatic/bouncr/infra/node_modules/@aws-cdk/aws-sns-subscriptions/lib/sqs.ts:55:24)

CDK CLI Version

1.143.0

Framework Version

1.143.0

Node.js Version

14.18.1

OS

Linux

Language

Typescript

Language Version

No response

Other information

It seems that related changes were made in 3cd8d481906fc4e3abdd1211908844e5b8bd2509 commit in @aws-cdk/aws-sns-subscriptions/lib/lambda.ts line 39.

@eoromb eoromb added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 4, 2022
@ryparker ryparker added the p2 label Feb 4, 2022
@jeffreyyoung
Copy link

A hacky workaround for this issue, is to create a new Topic.fromTopicArn, and add the subscription to the newly created topic.

So change this:

scope.topic.addSubscription(new SqsSubscription(queue, ...  ));  

to something like this:

const topicFromArn = new Topic.fromTopicArn(scope, 'topic-from-arn', scope.topic.topicArn);
topicFromArn.addSubscription(new SqsSubscription(queue, ...  )); 

@eoromb
Copy link
Author

eoromb commented Feb 7, 2022

@jeffreyyoung thanks for the proposed solution.

Just interesting if it is a bug or was intentionally done like this.

@jeffreyyoung
Copy link

Yeah, I'm not sure! Also not sure if the workaround I mentioned would cause other problems. 🤷‍♂️

@kaizencc
Copy link
Contributor

kaizencc commented Feb 9, 2022

I feel like this is a true circular dependency. Nested stacks cannot depend on their parents, so we can't depend on the topic existing in the nested stack. However, I feel like something like this should work, though I'm not very familiar with nested stacks:

Parent stack:

const topic = new Topic(this, 'topic', ...);
const nestedStack = new SomeNestedStack(this,...);
topic.addSubscription(new SqsSubscription(nestedStack.queue, ...));

Nested stack:

readonly queue: Queue;
constructor(scope: ParentStack, ...) {
   ...;  
  this.queue = new Queue(...);  
}

But this runs into the same error as above. @corymhall is this a use case that was considered in #17273? I don't have a strong understanding of our nested stack model; perhaps what i'm describing isn't possible either.

@eoromb
Copy link
Author

eoromb commented Feb 11, 2022

  1. Just want to mention that it does not matter where you call topic.addSubscription. In any case internal dependency between stacks is added.
  2. I seems that in this case it is necessary to create topic in NestedStack2 that way the dependency will be between nested stacks. This also means that no shared resources that are used in nested stacks, like VPC or so, are allowed to be created in the root stack.

@kaizencc kaizencc added effort/small Small work item – less than a day of effort and removed needs-triage This issue or PR still needs to be triaged. labels Mar 10, 2022
@kaizencc kaizencc removed their assignment Mar 10, 2022
@github-actions
Copy link

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Mar 10, 2023
@enheit
Copy link

enheit commented May 31, 2024

I have the same issue.

Whenever I pass ITopic to the nested stack...

new SpecialistLambdas(this, "SpecialistLambdas", {
  topic: this.beautyPageTopic
})

and use it like this

#registerEventHandler() {
	const subscription = new LambdaSubscription(this, {
	  filterPolicyWithMessageBody: {
		type: FilterOrPolicy.filter(SubscriptionFilter.stringFilter({
		  allowlist: [BeautyPageEventType.SpecialistLabelUpdated]
		}))
	  }
	})

	this.#props.beautyPageTopic.addSubscription(subscription)
}

It says the following issue

Error: Nested stack 'UglylookApiStack/SpecialistLambdas' cannot depend on a parent stack 'UglylookApiStack'
    at operateOnDependency (/home/enheit/Documents/projects/uglylook-api/node_modules/aws-cdk-lib/core/lib/deps.js:1:2128)
    at addDependency (/home/enheit/Documents/projects/uglylook-api/node_modules/aws-cdk-lib/core/lib/deps.js:1:489)
    at SpecialistLambdas.addDependency (/home/enheit/Documents/projects/uglylook-api/node_modules/aws-cdk-lib/core/lib/stack.js:1:8701)
    at LambdaSubscription.bind (/home/enheit/Documents/projects/uglylook-api/node_modules/aws-cdk-lib/aws-sns-subscriptions/lib/lambda.js:1:1529)
    at Topic.addSubscription (/home/enheit/Documents/projects/uglylook-api/node_modules/aws-cdk-lib/aws-sns/lib/topic-base.js:1:1256)
    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:52:33)
    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:110:5)
    at new SpecialistLambdas (/home/enheit/Documents/projects/uglylook-api/lib/lambdas/admin/specialist-lambdas.nested-stack.ts:40:54)
    at new UglyicelookApiStack (/home/enheit/Documents/projects/uglylook-api/lib/uglylook-api.stack.ts:284:5)

Subprocess exited with error 1

Is it still an issue or am I doing something wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-sns-subscriptions bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

No branches or pull requests

5 participants