Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda-event-sources): dead letter queue and filter policy for sns event source #10567

Merged
merged 2 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/@aws-cdk/aws-lambda-event-sources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ import * as sns from '@aws-cdk/aws-sns';
import { SnsEventSource } from '@aws-cdk/aws-lambda-event-sources';

const topic = new sns.Topic(...);
const deadLetterQueue = new sqs.Queue(this, 'deadLetterQueue');

lambda.addEventSource(new SnsEventSource(topic));
lambda.addEventSource(new SnsEventSource(topic, {
filterPolicy: { ... },
deadLetterQueue: deadLetterQueue
}));
```

When a user calls the SNS Publish API on a topic that your Lambda function is
Expand Down
13 changes: 11 additions & 2 deletions packages/@aws-cdk/aws-lambda-event-sources/lib/sns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ import * as lambda from '@aws-cdk/aws-lambda';
import * as sns from '@aws-cdk/aws-sns';
import * as subs from '@aws-cdk/aws-sns-subscriptions';

/**
* Properties forwarded to the Lambda Subscription.
*/
export interface SnsEventSourceProps extends subs.LambdaSubscriptionProps {
}

/**
* Use an Amazon SNS topic as an event source for AWS Lambda.
*/
export class SnsEventSource implements lambda.IEventSource {
constructor(readonly topic: sns.ITopic) {
private readonly props?: SnsEventSourceProps;

constructor(readonly topic: sns.ITopic, props?: SnsEventSourceProps) {
this.props = props;
}

public bind(target: lambda.IFunction) {
this.topic.addSubscription(new subs.LambdaSubscription(target));
this.topic.addSubscription(new subs.LambdaSubscription(target, this.props));
}
}
64 changes: 64 additions & 0 deletions packages/@aws-cdk/aws-lambda-event-sources/test/test.sns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, haveResource } from '@aws-cdk/assert';
import * as sns from '@aws-cdk/aws-sns';
import * as sqs from '@aws-cdk/aws-sqs';
import * as cdk from '@aws-cdk/core';
import { Test } from 'nodeunit';
import * as sources from '../lib';
Expand Down Expand Up @@ -47,4 +48,67 @@ export = {

test.done();
},

'props are passed to subscription'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const fn = new TestFunction(stack, 'Fn');
const topic = new sns.Topic(stack, 'T');
const queue = new sqs.Queue(stack, 'Q');
const props: sources.SnsEventSourceProps = {
deadLetterQueue: queue,
filterPolicy: {
Field: sns.SubscriptionFilter.stringFilter({
whitelist: ['A', 'B'],
}),
},
};

// WHEN
fn.addEventSource(new sources.SnsEventSource(topic, props));

// THEN
expect(stack).to(haveResource('AWS::Lambda::Permission', {
'Action': 'lambda:InvokeFunction',
'FunctionName': {
'Fn::GetAtt': [
'Fn9270CBC0',
'Arn',
],
},
'Principal': 'sns.amazonaws.com',
'SourceArn': {
'Ref': 'TD925BC7E',
},
}));

expect(stack).to(haveResource('AWS::SNS::Subscription', {
'Endpoint': {
'Fn::GetAtt': [
'Fn9270CBC0',
'Arn',
],
},
'Protocol': 'lambda',
'TopicArn': {
'Ref': 'TD925BC7E',
},
'FilterPolicy': {
'Field': [
'A',
'B',
],
},
'RedrivePolicy': {
'deadLetterTargetArn': {
'Fn::GetAtt': [
'Q63C6E3AB',
'Arn',
],
},
},
}));

test.done();
},
};