# LambdaSubscription

Use a Lambda function as a subscription target.

Implements: [ITopicSubscription](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_sns.ITopicSubscription.html)

- Source: [`aws-sns-subscriptions/lib/lambda.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-sns-subscriptions/lib/lambda.ts#L19)

## Example

TypeScript:

```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:

```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:

```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());
```

C# / .NET:

```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()) }
    }
}));
```

Golang:

```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

TypeScript:

```ts
new LambdaSubscription(fn: IFunction, props?: LambdaSubscriptionProps)
```

Python:

```python
LambdaSubscription(fn: IFunction, props: typing.Optional[LambdaSubscriptionProps] = None)
```

Java:

```java
new LambdaSubscription(IFunction fn, LambdaSubscriptionProps props)
```

C# / .NET:

```csharp
new LambdaSubscription(IFunction fn, LambdaSubscriptionProps? props = null)
```

Golang:

```go
NewLambdaSubscription(Fn IFunction, Props LambdaSubscriptionProps)
```

## Methods

- `bind(topic: ITopic): TopicSubscriptionConfig` — Returns a configuration for a Lambda function to subscribe to an SNS topic.
