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

Allow Fn::Join in SQS arn builder #5351

Merged
merged 3 commits into from
Mar 5, 2019
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
10 changes: 10 additions & 0 deletions docs/providers/aws/events/sqs.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ functions:
- sqs: - sqs:
arn: arn:
Fn::ImportValue: MyExportedQueueArnId Fn::ImportValue: MyExportedQueueArnId
- sqs:
arn:
Fn::Join:
- ":"
- - arn
- aws
- sqs
- Ref: AWS::Region
- Ref: AWS::AccountId
- MyOtherQueue
``` ```


## Setting the BatchSize ## Setting the BatchSize
Expand Down
6 changes: 5 additions & 1 deletion lib/plugins/aws/package/compile/events/sqs/index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class AwsCompileSQSEvents {
// for dynamic arns (GetAtt/ImportValue) // for dynamic arns (GetAtt/ImportValue)
if (Object.keys(event.sqs.arn).length !== 1 if (Object.keys(event.sqs.arn).length !== 1
|| !(_.has(event.sqs.arn, 'Fn::ImportValue') || !(_.has(event.sqs.arn, 'Fn::ImportValue')
|| _.has(event.sqs.arn, 'Fn::GetAtt'))) { || _.has(event.sqs.arn, 'Fn::GetAtt')
|| _.has(event.sqs.arn, 'Fn::Join'))) {
const errorMessage = [ const errorMessage = [
`Bad dynamic ARN property on sqs event in function "${functionName}"`, `Bad dynamic ARN property on sqs event in function "${functionName}"`,
' If you use a dynamic "arn" (such as with Fn::GetAtt or Fn::ImportValue)', ' If you use a dynamic "arn" (such as with Fn::GetAtt or Fn::ImportValue)',
Expand Down Expand Up @@ -84,6 +85,9 @@ class AwsCompileSQSEvents {
return EventSourceArn['Fn::GetAtt'][0]; return EventSourceArn['Fn::GetAtt'][0];
} else if (EventSourceArn['Fn::ImportValue']) { } else if (EventSourceArn['Fn::ImportValue']) {
return EventSourceArn['Fn::ImportValue']; return EventSourceArn['Fn::ImportValue'];
} else if (EventSourceArn['Fn::Join']) {
// [0] is the used delimiter, [1] is the array with values
return EventSourceArn['Fn::Join'][1].slice(-1).pop();
} }
return EventSourceArn.split(':').pop(); return EventSourceArn.split(':').pop();
}()); }());
Expand Down
69 changes: 62 additions & 7 deletions lib/plugins/aws/package/compile/events/sqs/index.test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -391,18 +391,29 @@ describe('AwsCompileSQSEvents', () => {
arn: { 'Fn::ImportValue': 'ForeignQueue' }, arn: { 'Fn::ImportValue': 'ForeignQueue' },
}, },
}, },
{
sqs: {
arn: {
'Fn::Join': [
':', [
'arn', 'aws', 'sqs', {
Ref: 'AWS::Region',
},
{
Ref: 'AWS::AccountId',
},
'MyQueue',
],
],
},
},
},
], ],
}, },
}; };


awsCompileSQSEvents.compileSQSEvents(); awsCompileSQSEvents.compileSQSEvents();


expect(awsCompileSQSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.FirstEventSourceMappingSQSSomeQueue.Properties.EventSourceArn
).to.deep.equal(
{ 'Fn::GetAtt': ['SomeQueue', 'Arn'] }
);
expect(awsCompileSQSEvents.serverless.service expect(awsCompileSQSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources.IamRoleLambdaExecution .provider.compiledCloudFormationTemplate.Resources.IamRoleLambdaExecution
.Properties.Policies[0].PolicyDocument.Statement[0] .Properties.Policies[0].PolicyDocument.Statement[0]
Expand All @@ -424,18 +435,62 @@ describe('AwsCompileSQSEvents', () => {
{ {
'Fn::ImportValue': 'ForeignQueue', 'Fn::ImportValue': 'ForeignQueue',
}, },
{
'Fn::Join': [
':',
[
'arn',
'aws',
'sqs',
{
Ref: 'AWS::Region',
},
{
Ref: 'AWS::AccountId',
},
'MyQueue',
],
],
},
], ],
} }
); );
expect(awsCompileSQSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.FirstEventSourceMappingSQSSomeQueue.Properties.EventSourceArn
).to.deep.equal(
{ 'Fn::GetAtt': ['SomeQueue', 'Arn'] }
);
expect(awsCompileSQSEvents.serverless.service expect(awsCompileSQSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources .provider.compiledCloudFormationTemplate.Resources
.FirstEventSourceMappingSQSForeignQueue.Properties.EventSourceArn .FirstEventSourceMappingSQSForeignQueue.Properties.EventSourceArn
).to.deep.equal( ).to.deep.equal(
{ 'Fn::ImportValue': 'ForeignQueue' } { 'Fn::ImportValue': 'ForeignQueue' }
); );
expect(awsCompileSQSEvents.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.FirstEventSourceMappingSQSMyQueue.Properties.EventSourceArn
).to.deep.equal(
{
'Fn::Join': [
':',
[
'arn',
'aws',
'sqs',
{
Ref: 'AWS::Region',
},
{
Ref: 'AWS::AccountId',
},
'MyQueue',
],
],
});
}); });


it('fails if keys other than Fn::GetAtt/ImportValue are used for dynamic queue ARN', () => { it('fails if keys other than Fn::GetAtt/ImportValue/Join are used for dynamic ARNs', () => {
awsCompileSQSEvents.serverless.service.functions = { awsCompileSQSEvents.serverless.service.functions = {
first: { first: {
events: [ events: [
Expand Down