Skip to content

Commit 7585a2e

Browse files
committed
fix: arn authorizers
1 parent 7057afe commit 7585a2e

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

lib/deploy/events/apiGateway/lambdaPermissions.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ const BbPromise = require('bluebird');
2424
const getFunctionLogicalId = (uri) => {
2525
const parts = uri['Fn::Join'][1];
2626
const functionRef = parts.find(x => _.has(x, 'Fn::GetAtt'));
27-
return functionRef['Fn::GetAtt'][0];
27+
return _.get(functionRef, "['Fn::GetAtt'][0]", parts[parts.length - 2]);
2828
};
2929

30+
const ARNRegex = /^arn:[^:\n]*:[^:\n]*:[^:\n]*:[^:\n]*:(?<resourceName>[^:/]*)[:/]?.*$/;
31+
3032
const getLambdaPermission = logicalId => ({
3133
Type: 'AWS::Lambda::Permission',
3234
Properties: {
33-
FunctionName: {
35+
FunctionName: ARNRegex.test(logicalId) ? logicalId : {
3436
'Fn::GetAtt': [logicalId, 'Arn'],
3537
},
3638
Action: 'lambda:InvokeFunction',
@@ -54,7 +56,14 @@ module.exports = {
5456
}
5557

5658
const lambdaPermissions = _.zipObject(
57-
funcLogicalIds.map(id => `${id}LambdaPermission`),
59+
funcLogicalIds.map((id) => {
60+
if (ARNRegex.test(id)) {
61+
const { groups: { resourceName } } = id.match(ARNRegex);
62+
return `${resourceName}LambdaPermission`;
63+
}
64+
65+
return `${id}LambdaPermission`;
66+
}),
5867
funcLogicalIds.map(getLambdaPermission),
5968
);
6069

lib/deploy/events/apiGateway/lambdaPermissions.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,55 @@ describe('#compileHttpLambdaPermissions()', () => {
152152
});
153153
});
154154
});
155+
156+
it('should create a Lambda Permission resource when there is an ARN authorizer', () => {
157+
serverlessStepFunctions.serverless.service.provider
158+
.compiledCloudFormationTemplate.Resources.HelloApiGatewayAuthorizer = {
159+
Type: 'AWS::ApiGateway::Authorizer',
160+
Properties: {
161+
AuthorizerResultTtlInSeconds: 300,
162+
IdentitySource: 'method.request.header.Authorization',
163+
Name: 'hello',
164+
RestApiId: {
165+
Ref: 'ApiGatewayRestApi',
166+
},
167+
AuthorizerUri: {
168+
'Fn::Join': [
169+
'',
170+
[
171+
'arn:',
172+
{
173+
Ref: 'AWS::Partition',
174+
},
175+
':apigateway:',
176+
{
177+
Ref: 'AWS::Region',
178+
},
179+
':lambda:path/2015-03-31/functions/',
180+
'arn:aws:lambda:us-east-1:000000000000:function:remote-authorizer',
181+
'/invocations',
182+
],
183+
],
184+
},
185+
Type: 'TOKEN',
186+
},
187+
};
188+
189+
serverlessStepFunctions.compileHttpLambdaPermissions().then(() => {
190+
const resources = serverlessStepFunctions.serverless.service.provider
191+
.compiledCloudFormationTemplate.Resources;
192+
const lambdaPermissions = _.values(resources).filter(x => x.Type === 'AWS::Lambda::Permission');
193+
expect(lambdaPermissions).to.have.lengthOf(1);
194+
expect(lambdaPermissions[0]).to.deep.eq({
195+
Type: 'AWS::Lambda::Permission',
196+
Properties: {
197+
FunctionName: 'arn:aws:lambda:us-east-1:000000000000:function:remote-authorizer',
198+
Action: 'lambda:InvokeFunction',
199+
Principal: {
200+
'Fn::Sub': 'apigateway.${AWS::URLSuffix}',
201+
},
202+
},
203+
});
204+
});
205+
});
155206
});

0 commit comments

Comments
 (0)