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

fix(sns-subscriptions): enable cross region subscriptions to sqs and lambda #17273

Merged
merged 3 commits into from Nov 11, 2021

Conversation

corymhall
Copy link
Contributor

fixing cross region subscriptions to SQS and Lambda. This PR handles a
couple different scenarios. This only applies to SNS topics that are
instanceof sns.Topic, it does not apply to imported topics (sns.ITopic).
The current behavior for imported topics will remain the same.

  1. SNS Topic and subscriber (sqs or lambda) are created in separate
    stacks with explicit environment.

In this case if the region is different between the two stacks then
the topic region will be provided in the subscription.

  1. SNS Topic and subscriber (sqs or lambda) are created in separate
    stacks, and both are env agnostic (no explicit region is provided)

In this case a region is not specified in the subscription resource,
which means it is assumed that they are both created in the same region.
The alternatives are to either throw an error telling the user to
specify a region, or to create a CfnOutput with the topic region. Since
it should be a rare occurrence for a user to be deploying a CDK app with
multiple env agnostic stacks that are meant for different environments,
I think the best option is to assume same region.

  1. SNS Topic and subscriber (sqs or lambda) are created in separate
    stacks, and one of them are env agnostic (no explicit region is provided)

In this case if the SNS stack has an explicit environment then we will
provide that in the subscription resource (assume that it is cross
region). If the SNS stack is env agnostic then we will do nothing
(assume they are created in the same region).

fixes #7044,#13707


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

lambda

fixing cross region subscriptions to SQS and Lambda. This PR handles a
couple different scenarios. This only applies to SNS topics that are
instanceof sns.Topic, it does not apply to imported topics (sns.ITopic).
The current behavior for imported topics will remain the same.

1. SNS Topic and subscriber (sqs or lambda) are created in separate
   stacks with explicit environment.

In this case if the `region` is different between the two stacks then
the topic region will be provided in the subscription.

2. SNS Topic and subscriber (sqs or lambda) are created in separate
   stacks, and _both_ are env agnostic (no explicit region is provided)

In this case a region is not specified in the subscription resource,
which means it is assumed that they are both created in the same region.
The alternatives are to either throw an error telling the user to
specify a region, or to create a CfnOutput with the topic region. Since
it should be a rare occurrence for a user to be deploying a CDK app with
multiple env agnostic stacks that are meant for different environments,
I think the best option is to assume same region.

3. SNS Topic and subscriber (sqs or lambda) are created in separate
   stacks, and _one_ of them are env agnostic (no explicit region is provided)

In this case if the SNS stack has an explicit environment then we will
provide that in the subscription resource (assume that it is cross
region). If the SNS stack is env agnostic then we will do nothing
(assume they are created in the same region).

fixes #7044,#13707
@gitpod-io
Copy link

gitpod-io bot commented Nov 2, 2021

@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Nov 2, 2021
@njlynch njlynch requested a review from a team November 4, 2021 15:54
@@ -36,6 +36,12 @@ export class LambdaSubscription implements sns.ITopicSubscription {
principal: new iam.ServicePrincipal('sns.amazonaws.com'),
});

// if the topic and function are created in different stacks
// then we need to make sure the topic is created first
if (topic.stack !== this.fn.stack) {
Copy link
Contributor

@njlynch njlynch Nov 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible this topic is an imported (ITopic)? If so, is this necessary?

My spidey sense tingles hard whenever we add a dependency like this; I feel like these types of aides lead to circular dependency bugs that aren't obvious and don't come to light until the next release. Just want to limit the blast radius to as small a use case as possible.

(Same question for the changes to sqs.ts, obviously.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I could add a type check here as well. I did run into issues when testing this if the dependency is not explicit, the deployment would fail because the function (or sqs) stack was getting created before the topic stack.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

Copy link
Contributor

@njlynch njlynch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@mergify
Copy link
Contributor

mergify bot commented Nov 11, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: dd60aa4
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 3cd8d48 into aws:master Nov 11, 2021
@mergify
Copy link
Contributor

mergify bot commented Nov 11, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Feb 21, 2022
…lambda (aws#17273)

fixing cross region subscriptions to SQS and Lambda. This PR handles a
couple different scenarios. This only applies to SNS topics that are
instanceof sns.Topic, it does not apply to imported topics (sns.ITopic).
The current behavior for imported topics will remain the same.

1. SNS Topic and subscriber (sqs or lambda) are created in separate
   stacks with explicit environment.

In this case if the `region` is different between the two stacks then
the topic region will be provided in the subscription.

2. SNS Topic and subscriber (sqs or lambda) are created in separate
   stacks, and _both_ are env agnostic (no explicit region is provided)

In this case a region is not specified in the subscription resource,
which means it is assumed that they are both created in the same region.
The alternatives are to either throw an error telling the user to
specify a region, or to create a CfnOutput with the topic region. Since
it should be a rare occurrence for a user to be deploying a CDK app with
multiple env agnostic stacks that are meant for different environments,
I think the best option is to assume same region.

3. SNS Topic and subscriber (sqs or lambda) are created in separate
   stacks, and _one_ of them are env agnostic (no explicit region is provided)

In this case if the SNS stack has an explicit environment then we will
provide that in the subscription resource (assume that it is cross
region). If the SNS stack is env agnostic then we will do nothing
(assume they are created in the same region).

fixes aws#7044,aws#13707


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SQS subscription to cross-region SNS topic not working via addSubscription
3 participants