Security News
NVD Backlog Tops 20,000 CVEs Awaiting Analysis as NIST Prepares System Updates
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
@aws-cdk/aws-sns
Advanced tools
@aws-cdk/aws-sns is an AWS Cloud Development Kit (CDK) library that allows you to define Amazon Simple Notification Service (SNS) topics and subscriptions in your AWS infrastructure as code. This package provides a high-level, object-oriented abstraction to create and manage SNS resources programmatically.
Create an SNS Topic
This code sample demonstrates how to create an SNS topic with a display name using the AWS CDK.
const sns = require('@aws-cdk/aws-sns');
const cdk = require('@aws-cdk/core');
class MyStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);
const topic = new sns.Topic(this, 'MyTopic', {
displayName: 'My Cool Topic'
});
}
}
const app = new cdk.App();
new MyStack(app, 'MyStack');
Add an Email Subscription to an SNS Topic
This code sample shows how to add an email subscription to an SNS topic using the AWS CDK.
const sns = require('@aws-cdk/aws-sns');
const subs = require('@aws-cdk/aws-sns-subscriptions');
const cdk = require('@aws-cdk/core');
class MyStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);
const topic = new sns.Topic(this, 'MyTopic');
topic.addSubscription(new subs.EmailSubscription('example@example.com'));
}
}
const app = new cdk.App();
new MyStack(app, 'MyStack');
Add a Lambda Subscription to an SNS Topic
This code sample demonstrates how to add a Lambda function as a subscription to an SNS topic using the AWS CDK.
const sns = require('@aws-cdk/aws-sns');
const subs = require('@aws-cdk/aws-sns-subscriptions');
const lambda = require('@aws-cdk/aws-lambda');
const cdk = require('@aws-cdk/core');
class MyStack extends cdk.Stack {
constructor(scope, id, props) {
super(scope, id, props);
const topic = new sns.Topic(this, 'MyTopic');
const myFunction = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda')
});
topic.addSubscription(new subs.LambdaSubscription(myFunction));
}
}
const app = new cdk.App();
new MyStack(app, 'MyStack');
The aws-sdk package is the official AWS SDK for JavaScript, which provides low-level APIs for interacting with AWS services, including SNS. Unlike @aws-cdk/aws-sns, which is used for defining infrastructure as code, aws-sdk is used for making API calls to AWS services at runtime.
The serverless framework is a toolkit for deploying and operating serverless architectures, including AWS Lambda and SNS. It provides a higher-level abstraction compared to @aws-cdk/aws-sns and is focused on deploying serverless applications rather than defining infrastructure as code.
Pulumi is an infrastructure as code tool that supports multiple cloud providers, including AWS. It allows you to define and manage cloud resources using familiar programming languages. Pulumi provides similar functionalities to @aws-cdk/aws-sns but offers a different approach and supports multiple clouds.
Add an SNS Topic to your stack:
const topic = new sns.Topic(this, 'Topic', {
displayName: 'Customer subscription topic',
});
Add a FIFO SNS topic with content-based de-duplication to your stack:
const topic = new sns.Topic(this, 'Topic', {
contentBasedDeduplication: true,
displayName: 'Customer subscription topic',
fifo: true,
topicName: 'customerTopic',
});
Note that FIFO topics require a topic name to be provided. The required .fifo
suffix will be automatically added to the topic name if it is not explicitly provided.
Various subscriptions can be added to the topic by calling the
.addSubscription(...)
method on the topic. It accepts a subscription object,
default implementations of which can be found in the
@aws-cdk/aws-sns-subscriptions
package:
Add an HTTPS Subscription to your topic:
const myTopic = new sns.Topic(this, 'MyTopic');
myTopic.addSubscription(new subscriptions.UrlSubscription('https://foobar.com/'));
Subscribe a queue to the topic:
declare const queue: sqs.Queue;
const myTopic = new sns.Topic(this, 'MyTopic');
myTopic.addSubscription(new subscriptions.SqsSubscription(queue));
Note that subscriptions of queues in different accounts need to be manually confirmed by reading the initial message from the queue and visiting the link found in it.
A filter policy can be specified when subscribing an endpoint to a topic.
Example with a Lambda subscription:
import * as lambda from '@aws-cdk/aws-lambda';
const myTopic = new sns.Topic(this, 'MyTopic');
declare const fn: lambda.Function;
// Lambda should receive only message matching the following conditions on attributes:
// color: 'red' or 'orange' or begins with 'bl'
// size: anything but 'small' or 'medium'
// price: between 100 and 200 or greater than 300
// store: attribute must be present
myTopic.addSubscription(new subscriptions.LambdaSubscription(fn, {
filterPolicy: {
color: sns.SubscriptionFilter.stringFilter({
allowlist: ['red', 'orange'],
matchPrefixes: ['bl'],
}),
size: sns.SubscriptionFilter.stringFilter({
denylist: ['small', 'medium'],
}),
price: sns.SubscriptionFilter.numericFilter({
between: { start: 100, stop: 200 },
greaterThan: 300,
}),
store: sns.SubscriptionFilter.existsFilter(),
},
}));
import { DeliveryStream } from '@aws-cdk/aws-kinesisfirehose';
const topic = new sns.Topic(this, 'Topic');
declare const stream: DeliveryStream;
new sns.Subscription(this, 'Subscription', {
topic,
endpoint: stream.deliveryStreamArn,
protocol: sns.SubscriptionProtocol.FIREHOSE,
subscriptionRoleArn: "SAMPLE_ARN", //role with permissions to send messages to a firehose delivery stream
});
CDK can attach provided Queue as DLQ for your SNS subscription. See the SNS DLQ configuration docs for more information about this feature.
Example of usage with user provided DLQ.
const topic = new sns.Topic(this, 'Topic');
const dlQueue = new sqs.Queue(this, 'DeadLetterQueue', {
queueName: 'MySubscription_DLQ',
retentionPeriod: Duration.days(14),
});
new sns.Subscription(this, 'Subscription', {
endpoint: 'endpoint',
protocol: sns.SubscriptionProtocol.LAMBDA,
topic,
deadLetterQueue: dlQueue,
});
SNS topics can be used as targets for CloudWatch event rules.
Use the @aws-cdk/aws-events-targets.SnsTopic
:
import * as codecommit from '@aws-cdk/aws-codecommit';
import * as targets from '@aws-cdk/aws-events-targets';
declare const repo: codecommit.Repository;
const myTopic = new sns.Topic(this, 'Topic');
repo.onCommit('OnCommit', {
target: new targets.SnsTopic(myTopic),
});
This will result in adding a target to the event rule and will also modify the topic resource policy to allow CloudWatch events to publish to the topic.
A topic policy is automatically created when addToResourcePolicy
is called, if
one doesn't already exist. Using addToResourcePolicy
is the simplest way to
add policies, but a TopicPolicy
can also be created manually.
const topic = new sns.Topic(this, 'Topic');
const topicPolicy = new sns.TopicPolicy(this, 'TopicPolicy', {
topics: [topic],
});
topicPolicy.document.addStatements(new iam.PolicyStatement({
actions: ["sns:Subscribe"],
principals: [new iam.AnyPrincipal()],
resources: [topic.topicArn],
}));
A policy document can also be passed on TopicPolicy
construction
const topic = new sns.Topic(this, 'Topic');
const policyDocument = new iam.PolicyDocument({
assignSids: true,
statements: [
new iam.PolicyStatement({
actions: ["sns:Subscribe"],
principals: [new iam.AnyPrincipal()],
resources: [topic.topicArn],
}),
],
});
const topicPolicy = new sns.TopicPolicy(this, 'Policy', {
topics: [topic],
policyDocument,
});
FAQs
The CDK Construct Library for AWS::SNS
The npm package @aws-cdk/aws-sns receives a total of 142,713 weekly downloads. As such, @aws-cdk/aws-sns popularity was classified as popular.
We found that @aws-cdk/aws-sns demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.