diff --git a/lib/plugins/aws/package/compile/events/sns/index.js b/lib/plugins/aws/package/compile/events/sns/index.js index 9b41dcf3bd8..681fb9b34b6 100644 --- a/lib/plugins/aws/package/compile/events/sns/index.js +++ b/lib/plugins/aws/package/compile/events/sns/index.js @@ -45,7 +45,7 @@ class AwsCompileSNSEvents { additionalProperties: false, }, }, - oneOf: [{ required: ['arn'] }, { required: ['topicName', 'displayName'] }], + oneOf: [{ required: ['arn'] }, { required: ['topicName'] }], additionalProperties: false, }, ], @@ -64,7 +64,6 @@ class AwsCompileSNSEvents { let topicArn; let topicName; let region; - let displayName = ''; let redrivePolicy; if (typeof event.sns === 'object') { if (event.sns.arn) { @@ -91,7 +90,6 @@ class AwsCompileSNSEvents { ); } } else { - displayName = event.sns.displayName; topicName = event.sns.topicName; } } else if (event.sns.indexOf('arn:') === 0) { @@ -229,13 +227,14 @@ class AwsCompileSNSEvents { }; if (!(topicLogicalId in template.Resources)) { + const topicResourceProperties = { TopicName: topicName }; + if (event.sns.displayName) { + topicResourceProperties.DisplayName = event.sns.displayName; + } _.merge(template.Resources, { [topicLogicalId]: { Type: 'AWS::SNS::Topic', - Properties: { - TopicName: topicName, - DisplayName: displayName, - }, + Properties: topicResourceProperties, }, }); } 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 286bd0b0eea..71d988e785c 100644 --- a/lib/plugins/aws/package/compile/events/sns/index.test.js +++ b/lib/plugins/aws/package/compile/events/sns/index.test.js @@ -87,6 +87,27 @@ describe('AwsCompileSNSEvents', () => { ).to.eql({ pet: ['dog', 'cat'] }); }); + it('should allow SNS topic without displayName', () => { + awsCompileSNSEvents.serverless.service.functions = { + first: { + events: [ + { + sns: { + topicName: 'Topic 1', + }, + }, + ], + }, + }; + + awsCompileSNSEvents.compileSNSEvents(); + + expect( + awsCompileSNSEvents.serverless.service.provider.compiledCloudFormationTemplate.Resources + .SNSTopicTopic1.Properties + ).to.to.not.have.property('DisplayName'); + }); + it('should create corresponding resources when topic is defined in resources', () => { awsCompileSNSEvents.serverless.service.functions = { first: {