Skip to content

bug: SNS cross region topic subscription fails (CloudFormation) #12670

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

Closed
1 task done
codenem opened this issue May 28, 2025 · 13 comments
Closed
1 task done

bug: SNS cross region topic subscription fails (CloudFormation) #12670

codenem opened this issue May 28, 2025 · 13 comments
Assignees
Labels
aws:sns Amazon Simple Notification Service status: in progress Currently being worked on type: bug Bug report

Comments

@codenem
Copy link

codenem commented May 28, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Starting version 4.1.0, subscribing to a topic from a different region fails with the following:

An error occurred (NotFound) when calling the Subscribe operation: Topic does not exist

I think the bug has been introduced by #12175, with the intention of raising the error for legit cases.

The code might be pretty self explanatory of the bug. If I understand correctly, here we should check the store corresponding to the region of the topic ARN we want to subscribe to, not the store of the region we are subscribing from.

Expected Behavior

Subscribing to a topic from a different region succeeds.

How are you starting LocalStack?

With a docker-compose file

Steps To Reproduce

How are you starting localstack (e.g., bin/localstack command, arguments, or docker-compose.yml)

services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}"
    image: localstack/localstack-pro:4.1.0 # required for Pro
    ports:
      - "127.0.0.1:4566:4566" # LocalStack Gateway
      - "127.0.0.1:4510-4559:4510-4559" # external services port range
      - "127.0.0.1:443:443" # LocalStack HTTPS Gateway (Pro)
    environment:
      # Activate LocalStack Pro: https://docs.localstack.cloud/getting-started/auth-token/
      - LOCALSTACK_AUTH_TOKEN=${LOCALSTACK_AUTH_TOKEN:?} # required for Pro
      # LocalStack configuration: https://docs.localstack.cloud/references/configuration/
      - DEBUG=${DEBUG:-0}
      - PERSISTENCE=${PERSISTENCE:-1}
      - ENFORCE_IAM=0
      - SNAPSHOT_LOAD_STRATEGY=ON_STARTUP
      - SNAPSHOT_SAVE_STRATEGY=ON_SHUTDOWN
    volumes:
      - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

Client commands (e.g., AWS SDK code snippet, or sequence of "awslocal" commands)

user@XYZ:~$ awslocal sqs create-queue --queue-name my-queue --region eu-west-1
{
    "QueueUrl": "http://sqs.eu-west-1.localhost.localstack.cloud:4566/000000000000/my-queue"
}
user@XYZ:~$ awslocal sns create-topic --name my-topic --region us-east-1
{
    "TopicArn": "arn:aws:sns:us-east-1:000000000000:my-topic"
}
user@XYZ:~$ awslocal sns subscribe --topic-arn "arn:aws:sns:us-east-1:000000000000:my-topic" --protocol sqs --notification-endpoint "arn:aws:sqs:eu-west-1:000000000000:my-queue" --region eu-west-1

An error occurred (NotFound) when calling the Subscribe operation: Topic does not exist

Environment

- OS: macOS Sequoia 15.5 (24F74) 
- LocalStack:
  LocalStack version: 4.1.0
  LocalStack Docker image sha: sha256:a60a3e7a1e9dd988bd42c7bf0b0355e0595a68b15cd24d6df3562583cb6da10a
  LocalStack build date: 2025-01-30
  LocalStack build git hash: 8a211b8ef2c35099bc6813ff24a962d32ddc402e

Anything else?

No response

@codenem codenem added type: bug Bug report status: triage needed Requires evaluation by maintainers labels May 28, 2025
@localstack-bot
Copy link
Collaborator

Welcome to LocalStack! Thanks for reporting your first issue and our team will be working towards fixing the issue for you or reach out for more background information. We recommend joining our Slack Community for real-time help and drop a message to LocalStack Support if you are a licensed user! If you are willing to contribute towards fixing this issue, please have a look at our contributing guidelines.

@Anze1508 Anze1508 added aws:sns Amazon Simple Notification Service status: backlog Triaged but not yet being worked on and removed status: triage needed Requires evaluation by maintainers labels May 28, 2025
@bentsku bentsku self-assigned this May 28, 2025
@bentsku bentsku added status: in progress Currently being worked on and removed status: backlog Triaged but not yet being worked on labels May 28, 2025
@bentsku
Copy link
Contributor

bentsku commented May 28, 2025

Hello @codenem and thanks for your report!

Just to confirm, is it possible to subscribe to a topic in Region A with a client configured in region B? I've just given it a try in AWS, and it raises an exception for me:

$ aws sqs create-queue --queue-name my-queue --region eu-west-1
{
    "QueueUrl": "https://eu-west-1.queue.amazonaws.com/<account-id>/my-queue"
}
$ aws sns create-topic --name my-topic --region us-east-1
{
    "TopicArn": "arn:aws:sns:us-east-1:<account-id>:my-topic"
}
$ aws sns subscribe --topic-arn "arn:aws:sns:us-east-1:<account-id>:my-topic" --protocol sqs --notification-endpoint "arn:aws:sqs:eu-west-1:<account-id>:my-queue" --region eu-west-1
An error occurred (InvalidParameter) when calling the Subscribe operation: Invalid parameter: TopicArn

The error message is different and I will fix that.

From the documentation, I believe the Subscribe call needs to be done in the same region as the topic lives:

The Amazon SNS subscription command must be executed in the region where Amazon SNS is hosted, in the corresponding region. For example, if Amazon SNS is in account "A" in the us-east-1 region, and the Lambda function is in account "B" in the us-east-2 region, the subscription CLI command must be executed in account "A" in the us-east-1 region.

https://docs.aws.amazon.com/sns/latest/dg/sns-cross-region-delivery.html

Please correct me if you have this exact setup working in AWS, but currently I'm unable to make it work.

Thanks!

@codenem
Copy link
Author

codenem commented May 28, 2025

Thanks for your answer @bentsku , indeed. I have provided those commands for quick reproduction, but we get the error from our CDK deployment on LocalStack (works on AWS). I'll double check the code and get back to you here, but we definitely get a different behavior (we use the same CloudFormation code).

@bentsku
Copy link
Contributor

bentsku commented May 28, 2025

@codenem, oh, in this case, this might a parameter we're not explicitly using in our CloudFormation handling. If you could share a small snippet from the template maybe, that could help debug the issue. Thanks again!

@bentsku
Copy link
Contributor

bentsku commented May 28, 2025

This might be somewhat related to aws/aws-cdk#13707, and we might not consider the CloudFormation Region field for the Subscription resource 👀

@codenem
Copy link
Author

codenem commented May 28, 2025

Yeah maybe, we're creating the subscription like so in Go:

queue := awssqs.NewQueue(
	defaultStack,
	namer.Format("queue"),
	&awssqs.QueueProps{
		QueueName: namer.Format("queue"),
	},
)

topic := awssns.NewTopic(
	stack,
	namer.Format("topic"),
	&awssns.TopicProps{
		EnforceSSL: aws.Bool(true),
		MasterKey:  masterKey,
		TopicName:  namer.Format("topic"),
	},
)

topic.AddSubscription(
	awssnssubscriptions.NewSqsSubscription(
		queue,
		&awssnssubscriptions.SqsSubscriptionProps{},
	),
)

I'm trying another approach, as for why it works in AWS, I guess they set the region now, or maybe when it is with an SQS queue (and not a lambda) that subscribes they have a different code branch.

@codenem
Copy link
Author

codenem commented May 28, 2025

Actually looking at the fix from the issue you linked, the subscription can either be in the topic's region or in the subscriber's (queue / lambda). So it seems that LS's implementation does not reflect this yet?

As for why the CLI command must subscribe in the same region, it is a separate topic than CDK.

@bentsku
Copy link
Contributor

bentsku commented May 28, 2025

@codenem yes, exactly. We did not support the Region parameter in the CloudFormation template for AWS::SNS::Subscription. I'm having a fix up at #12676, and this should fix your issue.

Looking at the fix for CDK you shared, it seems they have different code branch in CDK to automatically set the Region parameter in the generated CFN Template. This is definitely a CloudFormation issue in LocalStack to not consider this parameter, which leads to this failure.

If you could check that the AWS::SNS::Subscription in your generated CloudFormation template has the Region parameter set, so that we're sure we encounter the same error and that my fix will solve your issue? Thanks!

@codenem
Copy link
Author

codenem commented May 28, 2025

@bentsku I can confirm that for the subscriptions to a topic residing in another region than the subscriber, then the template shows the Region parameter on the subscription, and it is set to that of the topic (as per the PR from aws-cdk, since all my stacks have the region set in their env).

@bentsku
Copy link
Contributor

bentsku commented May 28, 2025

Awesome @codenem, then the PR linked will fix the issue for you once merged and available. Thanks for confirming! 🙏

I'll update you and ping you here once the fix is available in the latest Docker image.

@codenem codenem changed the title bug: SNS cross region topic subscription fails bug: SNS cross region topic subscription fails (CloudFormation) May 28, 2025
@bentsku
Copy link
Contributor

bentsku commented May 28, 2025

Hello again @codenem, the fix is now part of the latest image. Could you please run docker pull localstack/localstack-pro:latest, give it a try and see if that fixes your issue?

Thank you!

@codenem
Copy link
Author

codenem commented May 29, 2025

Hi @bentsku, I can confirm that using the latest pro image, the template now looks exactly as on the versions prior to 4.4.0 of the docker image, i.e. the Region is present in the generated template where needed on the subscriptions and the deployment is successful. 🙏

@bentsku
Copy link
Contributor

bentsku commented May 30, 2025

Awesome, thanks for confirming @codenem, and the help getting through the end of it! I'll close the issue, thanks again for the report 🙏

@bentsku bentsku closed this as completed May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aws:sns Amazon Simple Notification Service status: in progress Currently being worked on type: bug Bug report
Projects
None yet
Development

No branches or pull requests

4 participants