Skip to content

Commit

Permalink
feat(sns): add support for subscription DLQ in SNS
Browse files Browse the repository at this point in the history
### Commit Message
add support for subscription DLQ in SNS

fixes aws#7623
### End Commit Message

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
SeekerWing authored and karupanerura committed May 21, 2020
1 parent fa84dbd commit 2082269
Show file tree
Hide file tree
Showing 14 changed files with 470 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-sns-subscriptions/lib/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class EmailSubscription implements sns.ITopicSubscription {
endpoint: this.emailAddress,
protocol: this.props.json ? sns.SubscriptionProtocol.EMAIL_JSON : sns.SubscriptionProtocol.EMAIL,
filterPolicy: this.props.filterPolicy,
deadLetterQueue: this.props.deadLetterQueue,
};
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-sns-subscriptions/lib/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class LambdaSubscription implements sns.ITopicSubscription {
protocol: sns.SubscriptionProtocol.LAMBDA,
filterPolicy: this.props.filterPolicy,
region: this.regionFromArn(topic),
deadLetterQueue: this.props.deadLetterQueue,
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-sns-subscriptions/lib/sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class SqsSubscription implements sns.ITopicSubscription {
rawMessageDelivery: this.props.rawMessageDelivery,
filterPolicy: this.props.filterPolicy,
region: this.regionFromArn(topic),
deadLetterQueue: this.props.deadLetterQueue,
};
}

Expand Down
9 changes: 9 additions & 0 deletions packages/@aws-cdk/aws-sns-subscriptions/lib/subscription.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as sns from '@aws-cdk/aws-sns';
import { IQueue } from '@aws-cdk/aws-sqs';

/**
* Options to subscribing to an SNS topic
Expand All @@ -10,4 +11,12 @@ export interface SubscriptionProps {
* @default - all messages are delivered
*/
readonly filterPolicy?: { [attribute: string]: sns.SubscriptionFilter };

/**
* Queue to be used as dead letter queue.
* If not passed no dead letter queue is enabled.
*
* @default - No dead letter queue enabled.
*/
readonly deadLetterQueue?: IQueue;
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-sns-subscriptions/lib/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class UrlSubscription implements sns.ITopicSubscription {
protocol: this.protocol,
rawMessageDelivery: this.props.rawMessageDelivery,
filterPolicy: this.props.filterPolicy,
deadLetterQueue: this.props.deadLetterQueue,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,57 @@
"Echo11F3FB29",
"Arn"
]
},
"RedrivePolicy": {
"deadLetterTargetArn": {
"Fn::GetAtt": [
"DeadLetterQueue9F481546",
"Arn"
]
}
}
}
},
"DeadLetterQueue9F481546": {
"Type": "AWS::SQS::Queue",
"Properties": {
}
},
"DeadLetterQueuePolicyB1FB890C": {
"Type": "AWS::SQS::QueuePolicy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": "sqs:SendMessage",
"Condition": {
"ArnEquals": {
"aws:SourceArn": {
"Ref": "MyTopic86869434"
}
}
},
"Effect": "Allow",
"Principal": {
"Service": "sns.amazonaws.com"
},
"Resource": {
"Fn::GetAtt": [
"DeadLetterQueue9F481546",
"Arn"
]
}
}
],
"Version": "2012-10-17"
},
"Queues": [
{
"Ref": "DeadLetterQueue9F481546"
}
]
}
},
"FilteredServiceRole16D9DDC1": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as lambda from '@aws-cdk/aws-lambda';
import * as sns from '@aws-cdk/aws-sns';
import * as sqs from '@aws-cdk/aws-sqs';
import * as cdk from '@aws-cdk/core';
import * as subs from '../lib';

Expand All @@ -15,7 +16,9 @@ class SnsToLambda extends cdk.Stack {
code: lambda.Code.fromInline(`exports.handler = ${handler.toString()}`),
});

topic.addSubscription(new subs.LambdaSubscription(fction));
topic.addSubscription(new subs.LambdaSubscription(fction, {
deadLetterQueue: new sqs.Queue(this, 'DeadLetterQueue'),
}));

const fctionFiltered = new lambda.Function(this, 'Filtered', {
handler: 'index.handler',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,56 @@
"MyQueueE6CA6235",
"Arn"
]
},
"RedrivePolicy": {
"deadLetterTargetArn": {
"Fn::GetAtt": [
"DeadLetterQueue9F481546",
"Arn"
]
}
}
}
},
"DeadLetterQueue9F481546": {
"Type": "AWS::SQS::Queue",
"Properties": {
}
},
"DeadLetterQueuePolicyB1FB890C": {
"Type": "AWS::SQS::QueuePolicy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": "sqs:SendMessage",
"Condition": {
"ArnEquals": {
"aws:SourceArn": {
"Ref": "MyTopic86869434"
}
}
},
"Effect": "Allow",
"Principal": {
"Service": "sns.amazonaws.com"
},
"Resource": {
"Fn::GetAtt": [
"DeadLetterQueue9F481546",
"Arn"
]
}
}
],
"Version": "2012-10-17"
},
"Queues": [
{
"Ref": "DeadLetterQueue9F481546"
}
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class SnsToSqs extends cdk.Stack {
const topic = new sns.Topic(this, 'MyTopic');
const queue = new sqs.Queue(this, 'MyQueue');

topic.addSubscription(new subs.SqsSubscription(queue));
topic.addSubscription(new subs.SqsSubscription(queue, {
deadLetterQueue: new sqs.Queue(this, 'DeadLetterQueue'),
}));
/// !hide
}
}
Expand Down
Loading

0 comments on commit 2082269

Please sign in to comment.