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

Adds FilterPolicy to SNS event #5229

Merged
merged 4 commits into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/providers/aws/events/sns.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,20 @@ functions:
topicName: aggregate
displayName: Data aggregation pipeline
```

## Setting a filter policy

This event definition creates an SNS topic which subscription uses a filter policy. The filter policy filters out messages that don't have attribute key `pet` with value `dog` or `cat`.

```yml
functions:
pets:
handler: pets.handler
events:
- sns:
topicName: pets
filterPolicy:
pet:
- dog
- cat
```
35 changes: 27 additions & 8 deletions lib/plugins/aws/package/compile/events/sns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,18 @@ class AwsCompileSNSEvents {
'Fn::GetAtt': [lambdaLogicalId, 'Arn'],
};

if (topicArn) {
const subscriptionLogicalId = this.provider.naming
.getLambdaSnsSubscriptionLogicalId(functionName, topicName);
const subscriptionLogicalId = this.provider.naming
.getLambdaSnsSubscriptionLogicalId(functionName, topicName);

if (topicArn) {
_.merge(template.Resources, {
[subscriptionLogicalId]: {
Type: 'AWS::SNS::Subscription',
Properties: {
TopicArn: topicArn,
Protocol: 'lambda',
Endpoint: endpoint,
FilterPolicy: event.sns.filterPolicy,
},
},
});
Expand All @@ -134,6 +135,7 @@ class AwsCompileSNSEvents {
],
],
};

const topicLogicalId = this.provider.naming
.getTopicLogicalId(topicName);

Expand All @@ -142,21 +144,38 @@ class AwsCompileSNSEvents {
Protocol: 'lambda',
};

if (topicLogicalId in template.Resources) {
template.Resources[topicLogicalId]
.Properties.Subscription.push(subscription);
} else {
if (!(topicLogicalId in template.Resources)) {
_.merge(template.Resources, {
[topicLogicalId]: {
Type: 'AWS::SNS::Topic',
Properties: {
TopicName: topicName,
DisplayName: displayName,
Subscription: [subscription],
},
},
});
}

if (event.sns.filterPolicy) {
_.merge(template.Resources, {
[subscriptionLogicalId]: {
Type: 'AWS::SNS::Subscription',
Properties:
_.merge(subscription, {
TopicArn: {
Ref: topicLogicalId,
},
FilterPolicy: event.sns.filterPolicy,
}),
},
});
} else {
if (!template.Resources[topicLogicalId].Properties.Subscription) {
template.Resources[topicLogicalId].Properties.Subscription = [];
}
template.Resources[topicLogicalId]
.Properties.Subscription.push(subscription);
}
}

const lambdaPermissionLogicalId = this.provider.naming
Expand Down
122 changes: 118 additions & 4 deletions lib/plugins/aws/package/compile/events/sns/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ describe('AwsCompileSNSEvents', () => {
sns: {
topicName: 'Topic 1',
displayName: 'Display name for topic 1',
filterPolicy: {
pet: ['dog', 'cat'],
},
},
},
{
Expand All @@ -67,6 +70,67 @@ describe('AwsCompileSNSEvents', () => {
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.FirstLambdaPermissionTopic2SNS.Type
).to.equal('AWS::Lambda::Permission');
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.FirstSnsSubscriptionTopic1.Type
).to.equal('AWS::SNS::Subscription');
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate
.Resources.FirstSnsSubscriptionTopic1.Properties.FilterPolicy
).to.eql({ pet: ['dog', 'cat'] });
});

it('should create corresponding resources when topic is defined in resources', () => {
awsCompileSNSEvents.serverless.service.functions = {
first: {
events: [
{
sns: {
topicName: 'Topic 1',
displayName: 'Display name for topic 1',
filterPolicy: {
pet: ['dog', 'cat'],
},
},
},
{
sns: 'Topic 2',
},
],
},
};

Object.assign(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources, {
SNSTopicTopic2: {
Type: 'AWS::SNS::Topic',
Properties: {
TopicName: 'Topic 2',
DisplayName: 'Display name for topic 2',
},
},
});

awsCompileSNSEvents.compileSNSEvents();

expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.SNSTopicTopic1.Type
).to.equal('AWS::SNS::Topic');
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.SNSTopicTopic2.Type
).to.equal('AWS::SNS::Topic');
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.FirstLambdaPermissionTopic1SNS.Type
).to.equal('AWS::Lambda::Permission');
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.FirstLambdaPermissionTopic2SNS.Type
).to.equal('AWS::Lambda::Permission');
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.FirstSnsSubscriptionTopic1.Type
).to.equal('AWS::SNS::Subscription');
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate
.Resources.FirstSnsSubscriptionTopic1.Properties.FilterPolicy
).to.eql({ pet: ['dog', 'cat'] });
});

it('should create single SNS topic when the same topic is referenced repeatedly', () => {
Expand Down Expand Up @@ -94,10 +158,6 @@ describe('AwsCompileSNSEvents', () => {
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.SNSTopicTopic1.Type
).to.equal('AWS::SNS::Topic');
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.SNSTopicTopic1
.Properties.Subscription.length
).to.equal(2);
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.FirstLambdaPermissionTopic1SNS.Type
).to.equal('AWS::Lambda::Permission');
Expand Down Expand Up @@ -156,6 +216,10 @@ describe('AwsCompileSNSEvents', () => {
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.FirstLambdaPermissionFooSNS.Type
).to.equal('AWS::Lambda::Permission');
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate
.Resources.FirstSnsSubscriptionFoo.Properties.FilterPolicy
).to.equal(undefined);
});

it('should create SNS topic when only arn is given as an object property', () => {
Expand Down Expand Up @@ -184,6 +248,22 @@ describe('AwsCompileSNSEvents', () => {
).to.equal('AWS::Lambda::Permission');
});

it('should throw an error when the arn an object and the value is not a string', () => {
awsCompileSNSEvents.serverless.service.functions = {
first: {
events: [
{
sns: {
arn: 123,
},
},
],
},
};

expect(() => { awsCompileSNSEvents.compileSNSEvents(); }).to.throw(Error);
});

it('should create SNS topic when arn and topicName are given as object properties', () => {
awsCompileSNSEvents.serverless.service.functions = {
first: {
Expand All @@ -210,5 +290,39 @@ describe('AwsCompileSNSEvents', () => {
.provider.compiledCloudFormationTemplate.Resources.FirstLambdaPermissionBarSNS.Type
).to.equal('AWS::Lambda::Permission');
});

it('should create SNS topic when arn, topicName, and filterPolicy are given as object', () => {
awsCompileSNSEvents.serverless.service.functions = {
first: {
events: [
{
sns: {
topicName: 'bar',
arn: 'arn:aws:sns:region:accountid:bar',
filterPolicy: {
pet: ['dog', 'cat'],
},
},
},
],
},
};

awsCompileSNSEvents.compileSNSEvents();

expect(Object.keys(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources)
).to.have.length(2);
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.FirstSnsSubscriptionBar.Type
).to.equal('AWS::SNS::Subscription');
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate
.Resources.FirstSnsSubscriptionBar.Properties.FilterPolicy
).to.eql({ pet: ['dog', 'cat'] });
expect(awsCompileSNSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.FirstLambdaPermissionBarSNS.Type
).to.equal('AWS::Lambda::Permission');
});
});
});