Post

Subscribing SQS Queue to SNS Topic cross-account

Example stack for subscribing to topics from various accounts from a single consumer account:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from aws_cdk import aws_sns, aws_sqs, aws_lambda, aws_lambda_event_sources, aws_sns_subscriptions
import aws_cdk

TOPICS = {
    'my-event-eu': 'arn:aws:sns:eu-north-1:12345689012:my-event',
    'my-event-us': 'arn:aws:sns:us-east-1:34568901234:my-event',
}

class TopicSubscriptionStack(aws_cdk.Stack):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        log_lambda = aws_lambda.Function(
            scope=self,
            id="EventLogFunction",
            function_name="EventLogFunction",
            handler='main.lambda_handler',
            runtime=aws_lambda.Runtime.PYTHON_3_12,
            code=aws_lambda.Code.from_asset('./lambda'),
        )

        for event_name, topic_arn in TOPICS.items():
            topic = aws_sns.Topic.from_topic_arn(
                scope=self,
                id=f"Topic-{event_name}",
                topic_arn=topic_arn,
            )
            queue = aws_sqs.Queue(
                scope=self,
                id=f"EventQueue-{event_name}",
                queue_name=f'EventQueue-{event_name}',
                visibility_timeout=aws_cdk.Duration.seconds(60),
            )
            dlq = aws_sqs.Queue(
                scope=self,
                id=f"EventDLQ-{event_name}",
                queue_name=f'EventDLQ-{event_name}',
            )
            topic.add_subscription(
                aws_sns_subscriptions.SqsSubscription(
                    queue=queue,
                    dead_letter_queue=dlq,
                    raw_message_delivery=True,
                )
            )
            log_lambda.add_event_source(
                aws_lambda_event_sources.SqsEventSource(queue=queue)
            )
This post is licensed under CC BY 4.0 by the author.