https://serverless-stack.com/ logo
#help
Title
# help
g

Giovanni Giannichi

03/07/2022, 8:02 PM
Hi everyone! I’m trying to import an custom lambda authorizer between projects, I’ve tried many ways, like not setting an authorizer at all and hoping that the new routes will be created using the default setting in another project, or importing using the ARN like:
Copy code
const authorizerFunctionArn = ssm.StringParameter.valueFromLookup(
      this,
      'authorizerFunctionArn'
    );
    const authorizerFunction = lambda.Function.fromFunctionArn(
      this,
      'AccountsAuthorizerFunction',
      Lazy.string({ produce: () => authorizerFunctionArn })
    );
(Yes, I’ve tried without the lazy loading haha) In this case, the functions are successfully attached to the authorizer, but they never really call the function when I run some endpoint. Always resulting in a instant
Internal Server Error
… Any help about this will be awesome! My last hope was the
.fromFunctionName
, but this was added in CDK 2.14 (We are not there yet, I know! hahaha)
d

Derek Kershner

03/07/2022, 8:50 PM
Well, the (good?) news is that
.fromFunctionName
wont make a difference, it does the exact same thing as the arn, but makes a couple assumptions.
What are you attaching the authorizer to?
if it is an HTTP API Gateway, you should be able to reference your function as you are, and hand it in as
handler
to class HttpLambdaAuthorizer · AWS CDK (amazon.com)
you then use this as a prop for the api
g

Giovanni Giannichi

03/07/2022, 10:02 PM
Thank you Derek. Yeah, I had a feeling that the
fromFunctionName
won’t be a solution 😕 Anyway, I’m attaching the authorizer to an HTTP API Gateway imported from another project too, like this:
Copy code
const mainApiId = ssm.StringParameter.valueFromLookup(this, 'mainApiId');

    const httpApi = HttpApi.fromHttpApiAttributes(this, 'AccountsApi', {
      httpApiId: Lazy.string({ produce: () => mainApiId }),
    });
And this one seems to work, my routes are added without issues
Only the lambda authorizer that is attached but not executed… If I import the ARN hard-coded, it works 😕
d

Derek Kershner

03/07/2022, 11:00 PM
copy pasted arn instead of the ssm
valueFromLookup
works? That’s a new one…
you dead sure whats in the SSM is what you are copy pasting?
and that what is in
cdk.context.json
matches too?
g

Giovanni Giannichi

03/08/2022, 12:51 PM
Yes, I’m pretty sure it’s the same data, I’ve used some logs to check that, I was able to import the API using this method, only the authorizer failed… Anyway I’ve solved setting the fixed
functionName
in the original project and building manually the import path changing only what matters:
Copy code
const authorizerFunction = lambda.Function.fromFunctionArn(
      this,
      'ContactAuthorizerFunction',
      `arn:aws:lambda:${scope.region}:${scope.account}:function:mainAuthorizerFunction`
    );
2 Views