AWS CDK SNS Subscriptions (v1)

1.204.0 · maintenance · verified Fri Apr 17

This library provides AWS CDK constructs for defining various Amazon SNS subscription types (e.g., SQS, Lambda, Email, SMS). It is part of the AWS Cloud Development Kit (CDK) v1 ecosystem, allowing developers to define cloud infrastructure using familiar programming languages. The CDK project, and its associated modules, follow a rapid release cadence, often with weekly or bi-weekly updates for bug fixes and new features. This entry specifically covers version 1.x of the SNS subscriptions module.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create an AWS SNS topic and subscribe an SQS queue to it using the `aws-cdk-aws-sns-subscriptions` module. It defines a simple CDK stack, instantiates an SQS queue and an SNS topic, and then uses `subs.SqsSubscription` to link them. The `app.synth()` call generates the CloudFormation template.

import os
import aws_cdk as cdk
import aws_cdk.aws_sns as sns
import aws_cdk.aws_sqs as sqs
import aws_cdk.aws_sns_subscriptions as subs

# A basic CDK App and Stack
class MySnsSubscriptionStack(cdk.Stack):
    def __init__(self, scope: cdk.App, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # Create an SQS queue to be subscribed
        queue = sqs.Queue(self, "MySubscriptionQueue",
            queue_name="MyTestSubscriptionQueue",
            visibility_timeout=cdk.Duration.seconds(300)
        )

        # Create an SNS topic
        topic = sns.Topic(self, "MySubscriptionTopic",
            display_name="My Test Topic for Subscriptions"
        )

        # Subscribe the SQS queue to the SNS topic using SqsSubscription
        topic.add_subscription(
            subs.SqsSubscription(queue)
        )

        # Output the queue URL and topic ARN for verification
        cdk.CfnOutput(self, "QueueUrl", value=queue.queue_url)
        cdk.CfnOutput(self, "TopicArn", value=topic.topic_arn)

# Instantiate the CDK app and deployable stack
app = cdk.App()
MySnsSubscriptionStack(app, "MySnsSubscriptionStack")
app.synth() # Generates AWS CloudFormation template

view raw JSON →