From bb0ea88b95397607933263f55c8b368bb352061a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbyn=C4=9Bk=20Vymazal?= Date: Thu, 25 Jul 2019 12:38:44 +0200 Subject: [PATCH 1/5] Use declared SNS topic name --- lib/plugins/aws/package/compile/events/sns/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/aws/package/compile/events/sns/index.js b/lib/plugins/aws/package/compile/events/sns/index.js index b1fe06994..358733b1c 100644 --- a/lib/plugins/aws/package/compile/events/sns/index.js +++ b/lib/plugins/aws/package/compile/events/sns/index.js @@ -77,7 +77,7 @@ class AwsCompileSNSEvents { this.invalidPropertyErrorMessage(functionName, 'arn') ); } - topicName = topicName || event.sns.topicName; + topicName = event.sns.topicName || topicName; if (!topicName || typeof topicName !== 'string') { throw new this.serverless.classes.Error( this.invalidPropertyErrorMessage(functionName, 'topicName') From cf3df0e51276f3eea337e64802cabb198c2fa8af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbyn=C4=9Bk=20Vymazal?= Date: Thu, 25 Jul 2019 17:05:54 +0200 Subject: [PATCH 2/5] Add regression tests --- .../package/compile/events/sns/index.test.js | 70 ++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/lib/plugins/aws/package/compile/events/sns/index.test.js b/lib/plugins/aws/package/compile/events/sns/index.test.js index 18b72af71..6f065e26d 100644 --- a/lib/plugins/aws/package/compile/events/sns/index.test.js +++ b/lib/plugins/aws/package/compile/events/sns/index.test.js @@ -327,7 +327,7 @@ describe('AwsCompileSNSEvents', () => { }).to.throw(Error); }); - it('should create SNS topic when arn and topicName are given as object properties', () => { + it('should create SNS topic when both arn and topicName are given as object properties', () => { awsCompileSNSEvents.serverless.service.functions = { first: { events: [ @@ -358,6 +358,74 @@ describe('AwsCompileSNSEvents', () => { ).to.equal('AWS::Lambda::Permission'); }); + it('should create two SNS topic subsriptions for ARNs with the same topic name in two regions when different topicName parameters are specified', () => { + awsCompileSNSEvents.serverless.service.functions = { + first: { + events: [ + { + sns: { + topicName: 'first', + arn: 'arn:aws:sns:region-1:accountid:bar', + }, + }, + { + sns: { + topicName: 'second', + arn: 'arn:aws:sns:region-2:accountid:bar', + }, + } + ], + }, + }; + + awsCompileSNSEvents.compileSNSEvents(); + + expect( + Object.keys( + awsCompileSNSEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources + ) + ).to.have.length(4); + expect( + awsCompileSNSEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources + .FirstSnsSubscriptionFirst.Type + ).to.equal('AWS::SNS::Subscription'); + expect( + awsCompileSNSEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources + .FirstSnsSubscriptionSecond.Type + ).to.equal('AWS::SNS::Subscription'); + }); + + it('should override SNS topic subsription CF resource name when arn and topicName are given as object properties', () => { + awsCompileSNSEvents.serverless.service.functions = { + first: { + events: [ + { + sns: { + topicName: 'foo', + arn: 'arn:aws:sns:region:accountid:bar', + }, + }, + ], + }, + }; + + awsCompileSNSEvents.compileSNSEvents(); + + expect( + Object.keys( + awsCompileSNSEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources + ) + ).to.have.length(2); + expect( + awsCompileSNSEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources + .FirstSnsSubscriptionFoo.Type + ).to.equal('AWS::SNS::Subscription'); + expect( + awsCompileSNSEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources + .FirstLambdaPermissionFooSNS.Type + ).to.equal('AWS::Lambda::Permission'); + }); + // eslint-disable-next-line max-len it('should create SNS topic when arn object and topicName are given as object properties', () => { awsCompileSNSEvents.serverless.service.functions = { From 6269e930a1e5490c2474a6bea7f94c581b230dbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbyn=C4=9Bk=20Vymazal?= Date: Fri, 26 Jul 2019 11:06:14 +0200 Subject: [PATCH 3/5] Fix lint errors --- lib/plugins/aws/package/compile/events/sns/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/aws/package/compile/events/sns/index.test.js b/lib/plugins/aws/package/compile/events/sns/index.test.js index 6f065e26d..46fbc64d0 100644 --- a/lib/plugins/aws/package/compile/events/sns/index.test.js +++ b/lib/plugins/aws/package/compile/events/sns/index.test.js @@ -373,7 +373,7 @@ describe('AwsCompileSNSEvents', () => { topicName: 'second', arn: 'arn:aws:sns:region-2:accountid:bar', }, - } + }, ], }, }; From 822f8ea7e22c94e4c52c2e10752300e639dae72c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbyn=C4=9Bk=20Vymazal?= Date: Fri, 26 Jul 2019 11:20:02 +0200 Subject: [PATCH 4/5] Update documentation with an example --- docs/providers/aws/events/sns.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/providers/aws/events/sns.md b/docs/providers/aws/events/sns.md index fdc442a16..f9d8e8839 100644 --- a/docs/providers/aws/events/sns.md +++ b/docs/providers/aws/events/sns.md @@ -80,7 +80,20 @@ functions: topicName: MyCustomTopic ``` -**Note:** If an `arn` string is specified but not a `topicName`, the last substring starting with `:` will be extracted as the `topicName`. If an `arn` object is specified, `topicName` must be specified as a string, used only to name the underlying Cloudformation mapping resources. +**Note:** If an `arn` string is specified but not a `topicName`, the last substring starting with `:` will be extracted as the `topicName`. If an `arn` object is specified, `topicName` must be specified as a string, used only to name the underlying Cloudformation mapping resources. You can take advantage of this behavior when subscribing to multiple topics with the same name in different regions/accounts to avoid collisions between Cloudformation resource names. + +```yml +functions: + hello: + handler: handler.run + events: + - sns: + arn: arn:aws:sns:us-east-1:00000000000:topicname + topicName: topicname-account-1-us-east-1 + - sns: + arn: arn:aws:sns:us-east-1:11111111111:topicname + topicName: topicname-account-2-us-east-1 +``` ## Setting a display name From c6d8570db0892b5d0455c8511f2b9a99c2ef7c36 Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Fri, 26 Jul 2019 11:47:35 +0200 Subject: [PATCH 5/5] Fix indentation in docs --- docs/providers/aws/events/sns.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/providers/aws/events/sns.md b/docs/providers/aws/events/sns.md index f9d8e8839..b255c12aa 100644 --- a/docs/providers/aws/events/sns.md +++ b/docs/providers/aws/events/sns.md @@ -88,11 +88,11 @@ functions: handler: handler.run events: - sns: - arn: arn:aws:sns:us-east-1:00000000000:topicname - topicName: topicname-account-1-us-east-1 + arn: arn:aws:sns:us-east-1:00000000000:topicname + topicName: topicname-account-1-us-east-1 - sns: - arn: arn:aws:sns:us-east-1:11111111111:topicname - topicName: topicname-account-2-us-east-1 + arn: arn:aws:sns:us-east-1:11111111111:topicname + topicName: topicname-account-2-us-east-1 ``` ## Setting a display name