https://serverless-stack.com/ logo
#sst
Title
# sst
j

Jack Fraser

04/20/2021, 8:29 PM
Having reviewed the docs and this channel I am looking for a way to do custom logic for the
authorizationType
using a custom function? Similar to Serverless framework can have custom function for the
authorizer
. The reason for this is both for authorisation and to load in extra data onto the
event.requestContext.authorizer
r

Ross Coundon

04/20/2021, 8:30 PM
For one of our apps (the first I’m migrating) we use Firebase Auth via a custom lambda authorizer. You need to use ApiGatewayV1 for this
f

Frank

04/20/2021, 8:31 PM
@Jack Fraser which construct r u using?
Api
or
ApiGatewayV1Api
?
j

Jack Fraser

04/20/2021, 8:31 PM
Api
f

Frank

04/20/2021, 8:33 PM
You can either wait for CDK to support it and we can add support for it right after.
Or you can write some CloudFormation level construct to do it.
j

Jack Fraser

04/20/2021, 8:34 PM
would ApiGatewayV1Api support it? @Frank or is that legacy?
f

Frank

04/20/2021, 8:34 PM
ApiGatewayV1Api
does support it. But imo it’s not worth switching to
ApiGatewayV1Api
just for this.
R u blocked by this?
I can help u put something together for now using the CfnAuthorizer construct until CDK supports it.
j

Jack Fraser

04/21/2021, 10:26 AM
Hi @Frank to adopt it easier it would great if we did not have to change how we currently do it
f

Frank

04/21/2021, 5:14 PM
By “did not have to change how we currently do it”, do you mean you want to keep using the
Api
construct?
j

Jack Fraser

04/22/2021, 8:37 AM
thanks @Frank - not worth using older ApiGatewayV1Api for this. Will work round it for now. Do you know when the CDK/SST will support custom auths?
f

Frank

04/22/2021, 4:30 PM
Hey Jack, we are watching this PR on the CDK side https://github.com/aws/aws-cdk/pull/13181
It seems the initial implementation has been submitted, and it’s going through the review process.
But if u need to get this to work before CDK supports it, u can write some CloudFormation code in your CDK app to do this (custom authorizer is supported by CF, just not CDK) . If you plan to do that, let me know, I can help you on that. 🙂
j

Jack Fraser

05/17/2021, 5:06 PM
@Frank seems like the CDK now supports this
f

Frank

05/17/2021, 5:39 PM
Nice! Once they cut a release with it, we can add the support to SST.
Hey @Jack Fraser, just aded Lambda authorizer to v0.20.0:
Copy code
new Api(this, "Api", {
  defaultAuthorizationType: ApiAuthorizationType.CUSTOM,
  defaultAuthorizer: new HttpLambdaAuthorizer({
    authorizerName: "LambdaAuthorizer",
    handler: new Function(this, "Authorizer", {
      handler: "src/authorizer.main",
    }),
  }),
  routes: {
    "GET /notes": "src/list.main",
  },
});
j

Jack Fraser

07/05/2021, 3:30 PM
@Frank - great work adding this. What is the best way to have a single authorizer handler function for multiple stacks in the same app?
is it possible to use
getFunction
etc but a path of another stack?
f

Frank

07/05/2021, 6:12 PM
Hey @Jack Fraser, you just need to set the defaultAuthorizer on the Api, and it applies to all routes across stacks. I just added a section in the doc on this topic https://docs.serverless-stack.com/constructs/Api#sharing-an-api-authorizer
You should be able to call
getFunction
across stacks. Give it a try. Let me know if it doesn’t work for you.