Skip to content

Commit

Permalink
feat(sns): fifo topic with content-based deduplication support aws#11127
Browse files Browse the repository at this point in the history
 (aws#11588)

Add support for FIFO and content-based deduplication in SNS topics.

Closes aws#11127 
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rrhodes authored and flochaz committed Jan 5, 2021
1 parent 53d9ffd commit a5189d0
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/@aws-cdk/aws-sns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ const topic = new sns.Topic(this, 'Topic', {
});
```

Add a FIFO SNS topic with content-based de-duplication to your stack:

```ts
import * as sns from '@aws-cdk/aws-sns';

const topic = new sns.Topic(this, 'Topic', {
contentBasedDeduplication: true,
displayName: 'Customer subscription topic',
fifo: true,
});
```

## Subscriptions

Various subscriptions can be added to the topic by calling the
Expand Down
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-sns/lib/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ export interface TopicProps {
* @default None
*/
readonly masterKey?: IKey;

/**
* Enables content-based deduplication for FIFO topics.
*
* @default None
*/
readonly contentBasedDeduplication?: boolean;

/**
* Set to true to create a FIFO topic.
*
* @default None
*/
readonly fifo?: boolean;
}

/**
Expand Down Expand Up @@ -66,10 +80,16 @@ export class Topic extends TopicBase {
physicalName: props.topicName,
});

if (props.contentBasedDeduplication && !props.fifo) {
throw new Error('Content based deduplication can only be enabled for FIFO SNS topics.');
}

const resource = new CfnTopic(this, 'Resource', {
displayName: props.displayName,
topicName: this.physicalName,
kmsMasterKeyId: props.masterKey && props.masterKey.keyArn,
contentBasedDeduplication: props.contentBasedDeduplication,
fifoTopic: props.fifo,
});

this.topicArn = this.getResourceArnAttribute(resource.ref, {
Expand Down
13 changes: 13 additions & 0 deletions packages/@aws-cdk/aws-sns/test/integ.sns-fifo.expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Resources": {
"MyTopic86869434": {
"Type": "AWS::SNS::Topic",
"Properties": {
"ContentBasedDeduplication": true,
"DisplayName": "fooDisplayName",
"FifoTopic": true,
"TopicName": "fooTopic.fifo"
}
}
}
}
21 changes: 21 additions & 0 deletions packages/@aws-cdk/aws-sns/test/integ.sns-fifo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { App, Stack, StackProps } from '@aws-cdk/core';
import { Topic } from '../lib';

class SNSFifoInteg extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
super(scope, id, props);

new Topic(this, 'MyTopic', {
topicName: 'fooTopic.fifo',
displayName: 'fooDisplayName',
contentBasedDeduplication: true,
fifo: true,
});
}
}

const app = new App();

new SNSFifoInteg(app, 'SNSFifoInteg');

app.synth();
54 changes: 54 additions & 0 deletions packages/@aws-cdk/aws-sns/test/test.sns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,60 @@ export = {

test.done();
},

'specify fifo without contentBasedDeduplication'(test: Test) {
const stack = new cdk.Stack();

new sns.Topic(stack, 'MyTopic', {
fifo: true,
});

expect(stack).toMatch({
'Resources': {
'MyTopic86869434': {
'Type': 'AWS::SNS::Topic',
'Properties': {
'FifoTopic': true,
},
},
},
});

test.done();
},

'specify fifo with contentBasedDeduplication'(test: Test) {
const stack = new cdk.Stack();

new sns.Topic(stack, 'MyTopic', {
contentBasedDeduplication: true,
fifo: true,
});

expect(stack).toMatch({
'Resources': {
'MyTopic86869434': {
'Type': 'AWS::SNS::Topic',
'Properties': {
'ContentBasedDeduplication': true,
'FifoTopic': true,
},
},
},
});

test.done();
},

'throw with contentBasedDeduplication on non-fifo topic'(test: Test) {
const stack = new cdk.Stack();

test.throws(() => new sns.Topic(stack, 'MyTopic', {
contentBasedDeduplication: true,
}), /Content based deduplication can only be enabled for FIFO SNS topics./);

test.done();
},
},

'can add a policy to the topic'(test: Test) {
Expand Down

0 comments on commit a5189d0

Please sign in to comment.