Skip to content
v2.275.0

LambdaSubscription Class ​

Use a Lambda function as a subscription target.

Example ​

ts
import * as lambda from 'aws-cdk-lib/aws-lambda';

const myTopic = new sns.Topic(this, 'MyTopic');
declare const fn: lambda.Function;

// Lambda should receive only message matching the following conditions on message body:
// color: 'red' or 'orange'
// store: property must not be present
myTopic.addSubscription(new subscriptions.LambdaSubscription(fn, {
  filterPolicyWithMessageBody: {
    background: sns.FilterOrPolicy.policy({
      color: sns.FilterOrPolicy.filter(sns.SubscriptionFilter.stringFilter({
        allowlist: ['red', 'orange'],
      })),
    }),
    store: sns.FilterOrPolicy.filter(sns.SubscriptionFilter.notExistsFilter()),
  },
}));
python
import aws_cdk.aws_lambda as lambda_
# fn: lambda.Function


my_topic = sns.Topic(self, "MyTopic")

# Lambda should receive only message matching the following conditions on message body:
# color: 'red' or 'orange'
# store: property must not be present
my_topic.add_subscription(subscriptions.LambdaSubscription(fn,
    filter_policy_with_message_body={
        "background": sns.FilterOrPolicy.policy({
            "color": sns.FilterOrPolicy.filter(sns.SubscriptionFilter.string_filter(
                allowlist=["red", "orange"]
            ))
        }),
        "store": sns.FilterOrPolicy.filter(sns.SubscriptionFilter.not_exists_filter())
    }
))
java
import software.amazon.awscdk.services.lambda.*;
Function fn;


Topic myTopic = new Topic(this, "MyTopic");

// Lambda should receive only message matching the following conditions on message body:
// color: 'red' or 'orange'
// store: property must not be present
myTopic.addSubscription(LambdaSubscription.Builder.create(fn)
        .filterPolicyWithMessageBody(Map.of(
                "background", FilterOrPolicy.policy(Map.of(
                        "color", FilterOrPolicy.filter(SubscriptionFilter.stringFilter(StringConditions.builder()
                                .allowlist(List.of("red", "orange"))
                                .build())))),
                "store", FilterOrPolicy.filter(SubscriptionFilter.notExistsFilter())))
        .build());
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.Lambda;
Function fn;


var myTopic = new Topic(this, "MyTopic");

// Lambda should receive only message matching the following conditions on message body:
// color: 'red' or 'orange'
// store: property must not be present
myTopic.AddSubscription(new LambdaSubscription(fn, new LambdaSubscriptionProps {
    FilterPolicyWithMessageBody = new Dictionary<string, FilterOrPolicy> {
        { "background", FilterOrPolicy.Policy(new Dictionary<string, FilterOrPolicy> {
            { "color", FilterOrPolicy.Filter(SubscriptionFilter.StringFilter(new StringConditions {
                Allowlist = new [] { "red", "orange" }
            })) }
        }) },
        { "store", FilterOrPolicy.Filter(SubscriptionFilter.NotExistsFilter()) }
    }
}));
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import lambda "github.com/aws/aws-cdk-go/awscdk"
var fn Function


myTopic := sns.NewTopic(this, jsii.String("MyTopic"))

// Lambda should receive only message matching the following conditions on message body:
// color: 'red' or 'orange'
// store: property must not be present
myTopic.AddSubscription(subscriptions.NewLambdaSubscription(fn, &LambdaSubscriptionProps{
	FilterPolicyWithMessageBody: map[string]FilterOrPolicy{
		"background": sns.FilterOrPolicy_policy(map[string]FilterOrPolicy{
			"color": sns.FilterOrPolicy_filter(sns.SubscriptionFilter_stringFilter(&StringConditions{
				"allowlist": []*string{
					jsii.String("red"),
					jsii.String("orange"),
				},
			})),
		}),
		"store": sns.FilterOrPolicy_filter(sns.SubscriptionFilter_notExistsFilter()),
	},
}))

Initializer ​

ts
new LambdaSubscription(fn: IFunction, props?: LambdaSubscriptionProps)
python
LambdaSubscription(fn: IFunction, props: typing.Optional[LambdaSubscriptionProps] = None)
java
new LambdaSubscription(IFunction fn, LambdaSubscriptionProps props)
csharp
new LambdaSubscription(IFunction fn, LambdaSubscriptionProps? props = null)
go
NewLambdaSubscription(Fn IFunction, Props LambdaSubscriptionProps)

Methods ​

bind(topic: ITopic): TopicSubscriptionConfigdef bind(self, topic: ITopic) -> TopicSubscriptionConfigpublic TopicSubscriptionConfig bind(ITopic topic)public TopicSubscriptionConfig Bind(ITopic topic)func Bind(Topic ITopic) TopicSubscriptionConfig#
Returns a configuration for a Lambda function to subscribe to an SNS topic.