AWS API Gateway: Lack of support for CF intrinsic functions at `functions[].events[].http.authorizer.arn`
- What was the config you used?
functions:
myFunction:
handler: handler.myFunction
events:
- http:
...
authorizer:
arn: { "Fn::Join" : [ ":", [ "arn:aws:lambda", { "Ref" : "AWS::Region" }, { "Ref" : "AWS::AccountId" }, "function:myAuthorizer" ] ] }
...
- What stacktrace or error message from your provider did you see?
Serverless: Packaging service...
Type Error ---------------------------------------------
functionArn.split is not a function
For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Please report this error. We think it might be a bug.
Your Environment Information -----------------------------
OS: darwin
Node Version: 4.3.2
Serverless Version: 1.6.1
Similar or dependent issues:
- #3129
Additional Data
- Serverless Framework Version you're using: 1.6.1
- Operating System: macOS Sierra 10.12.3
Proposed solution:
This report signals two issues:
1. Messed up error reporting (functionArn.split is not a function)
No need to do anything here, as such errors will be handled neatly, once schema is configured for this event (see: https://github.com/serverless/serverless/issues/8018)
2. No support for CF intrinsic functions at authorizer.arn.
See: https://github.com/serverless/serverless/issues/3212#issuecomment-696637248
- When
arnis passed in object form, requirenameproperty (validate it's existence inline, at least it might be hard to validate it with schema, and receive human friendly error message). - When
arnis passed in string form and nonameis passed. deprecate name resolution with message that it won't be auto-resolved in next major and to have authorizer named,namehas to be explicitly set in config
Similar issue: One project that is the global authorizer, and exports lambda function ARNs
Other project that want to use that authorizer
authorizer:
apn:
Fn::ImportValue: ${self:authService}:${opt:stage, self:provider.stage}:AuthLambdaARN
This yields the same problem as the naming cannot be determined from this. Only other option is to hardcode the ARN for now.
@eahefnawy @pmuens This is a similar issue as the arn parsing in the stream arns. I think we need a general arn parser function somewhere that is capable to (1) read string formatted arns (2) read arbitrary dynamic arns (Fn::Join, Fn::GetAttr, Ref) And return the reference resource logical id. It makes no sense to fix the problem in various places separately with different implementations.
I suggest to have one parseArn() function in serverless.utils that would be used from everywhere.
In my plugin I used a very generic function for that:
static findAllReferences(root) {
const resourceRefs = [];
const stack = [ { parent: null, value: root, path: '' } ];
while (!_.isEmpty(stack)) {
const property = stack.pop();
_.forOwn(property.value, (value, key) => {
if (key === 'Ref') {
resourceRefs.push({ ref: value, path: property.path });
} else if (key === 'Fn::GetAtt') {
resourceRefs.push({ ref: value[0], path: property.path });
} else if (_.isObject(value)) {
key = _.isArray(property.value) ? `[${key}]` : (_.isEmpty(property.path) ? `${key}` : `.${key}`);
stack.push({ parent: property, value, path: `${property.path}${key}` });
}
});
}
return resourceRefs;
}
Given a root object (in this case the arn: object) it will return an array of all contained references (including those contained within GetAtt) with their path. You can use _.get(root, refPath) to access the ref object or just use the returned refs (which are the logical resource ids).
Can someone link the other issue as related here?
@HyperBrain love the idea of having a central ARN parser!
What do you think about opening up a separate issue for this. Or should we rename this one?
This implementation will also be affected by the ARN parser: https://github.com/serverless/serverless/pull/3111#issuecomment-285158339
@pmuens I think we should open a separate issue and link all existing arn parser exception issues there. Then it is clear for every single issue that it will be handled centrally.
Link to https://github.com/serverless/serverless/issues/3212#issuecomment-285731535 as the s3 event bucket specification could also be a generic ARN as an alternative to create the bucket implicitly. This would make the s3 events much more flexible.
https://github.com/serverless/serverless/pull/3309
Adding #3162 here (as brought up by @HyperBrain)
Adding a good example brought up by @erikerikson in the context of step functions:
Resource: [lambda name | activity name | lambda ARN | activity ARN | Fn::GetAtt (lambda) | Fn::Ref (activity) | Fn::ImportValue]
So the Arn parser should support the following declarations and parse them correctly:
- lambda name
- Fully qualified ARN (arn:
:....) - Fn::Ref (resource id)
- Fn::GetAtt (resource id),(identifier)
- Fn::Join (containing a split arn possibly with Fn::Ref, Fn::GetAtt or Fn::ImportValue as parts)
- Fn::ImportValue
- Fn::Sub
Any additions are welcome.
Thanks for updating @HyperBrain
Here's the link to the comment mentioned above 🔝 --> https://github.com/serverless/serverless/issues/3024#issuecomment-291334434
Perhaps I'm missing something; but isn't this issue also about referencing resources from within the same stack?
I have a cognito user pool in my Resources section, and I would like to reference it as an authorizer using Fn::GetAtt but the way the code is written today, this is not possible, right?
ie this is no just for cross stack referencing, is it? Or is there a way of doing what i want that I'm missing?
This task is for a general universal ARN parser that allows specifying references as ARN string, Fn::GetAtt, Ref or whatever notations there are to reference resources. It should unify the references, may it be event declarations or something else. So you're completely right - the initial reason for the task was, that multiple places where in-stack resources had been referenced where/are implemented differently in the different locations with the effect that e.g. some notations work in one place but not in other places.
Ok; and to be clear; right now there's no way to define a Cognito UserPool in the Resources section and then have the functions use events that auth against that newly created user pool, since there's no way to get to the ARN of that created pool; correct?
As HyperBrain notes, ARN parser was the original thought this was created under (the stream attribute was used for both Kinesis and Dynamo streams) but it is definitely intended to be a generalized resource reference implementation.
The reason for this is current inconsistency in the implementation. That sometimes you can use Ref or ImportValue, et cetera but not others. For a specific case, try it and see.
As HyperBrain notes, ARN parser was the original thought this was created under (the stream attribute was used for both Kinesis and Dynamo streams) but it is definitely intended to be a generalized resource reference implementation.
The reason for this is current inconsistency in the implementation. That sometimes you can use Ref or ImportValue, et cetera but not others. For a specific case, try it and see.
@erikerikson comments are a pretty good summary of the purpose for the arn parser.
@jliebrand have you used the new cognitoUserPool event source (released in v1.15) to achieve that? Or are you using another solution right now?
@pmuens no, I wasn't aware of any cognito event source... What I do now is first deploy my stack with the functions having simply no authorizer at all... Then once the stack is deployed for each of my stages, I add these custom vars to the top of my yml:
custom:
stagingUserPool: arn:aws:cognito-idp:xxxOne
masterUserPool: arn:aws:cognito-idp:xxxTwo
devUserPool: arn:aws:cognito-idp:xxxThree
And then add these authorizers:
authorizer:
arn: ${self:custom.${opt:stage}UserPool}
This will obviously break if I ever remove and re-deploy a stack, so it's far from ideal
Interesting solution. Thanks for sharing 👍
We've just added the cognitoUserPool event source. Only supports triggers right now though...
Just chiming in to say a global ARN parser would be incredibly useful.
With #3657 we can define user pools, but we can't reference their ARNs as Lambda authorizers. This unfortunately means either having to split a stack or deploy once without authorizers and then again after retrieving and setting the ARN 😞
I'm also happy it would implement this. With my https://github.com/horike37/serverless-step-functions plugin, you need describe Lambda ARN directly, so not userfriendly.. Therefore I'd like to use a global ARN parser and would also like to reference Lambda functions ARN
A general issue that we run into is that an intrinsic function of choice(GetAttr, Sub, Split, Join, Ref, Import, etc) is seemingly randomly unavailable. It feel like there may be a bit of unfortunate one-off logic for config parsing going. Will the "ARN parser" work allow the intrinsic functions to work all the places one would expect(everywhere)?
That is exactly the intent (as valid) @protip