I created a shared API gateway using this following example.
Then implemented Auth0 using the following as the base.
I was trying to share this custom authorizer across other lambda scripts as well.
But I am not sure how that can be done.
SharedGateway serverless.yml
provider:
name: aws
runtime: nodejs12.x
region: ap-south-1
stage: dev
profile: grabup-dev
environment:
AUTH0_CLIENT_ID: ${file(./secrets.json):AUTH0_CLIENT_ID}
AUTH0_CLIENT_PUBLIC_KEY: ${file(./public_key)}
resources:
Resources:
SharedGW:
Type: AWS::ApiGateway::RestApi
Properties:
Name: SharedGW
GatewayResponse:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: EXPIRED_TOKEN
RestApiId:
Ref: SharedGW
StatusCode: '401'
AuthFailureGatewayResponse:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: UNAUTHORIZED
RestApiId:
Ref: SharedGW
StatusCode: '401'
AuthorizerPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Fn::GetAtt: AuthorizerLambdaFunction.Arn
Action: lambda:InvokeFunction
Principal:
Fn::Join: ["",["apigateway.", { Ref: "AWS::URLSuffix"}]]
Authorizer:
DependsOn:
- SharedGW
Type: AWS::ApiGateway::Authorizer
Properties:
Name: ${self:provider}-Authorizer
RestApiId: { "Ref": "SharedGW" }
Type: TOKEN
IdentitySource: method.request.header.Authorization
AuthorizerResultTtlInSeconds: 300
AuthorizerUri:
Fn::Join:
- ''
- - 'arn:aws:apigateway:'
- Ref: "AWS::Region"
- ':lambda:path/2015-03-31/functions/'
- Fn::GetAtt: "AuthorizerLambdaFunction.Arn"
- "/invocations"
Outputs:
apiGatewayRestApiId:
Value:
Ref: SharedGW
Export:
Name: SharedGW-restApiId
apiGatewayRestApiRootResourceId:
Value:
Fn::GetAtt:
- SharedGW
- RootResourceId
Export:
Name: SharedGW-rootResourceId
AuthorizerId:
Value:
Ref: Authorizer
Export:
Name: SharedGW-authorizerId
functions:
authorizer:
handler: handler.auth
publicEndpoint:
handler: handler.publicEndpoint
events:
- http:
path: api/public
method: get
cors: true
privateEndpoint:
handler: handler.privateEndpoint
events:
- http:
path: api/private
method: get
authorizer:
type: CUSTOM
authorizerId:
Ref: Authorizer
cors: true
My other products service yml file looks like the below
provider:
name: aws
runtime: nodejs12.x
region: ap-south-1
stage: dev
profile: grabup-dev
apiGateway:
restApiId:
"Fn::ImportValue": SharedGW-restApiId
restApiRootResourceId:
"Fn::ImportValue": SharedGW-rootResourceId
functions:
get-products:
handler: handler.getProducts
events:
- http:
path: products/list
method: get
authorizer:
type: CUSTOM
authorizerId: !ImportValue SharedGW-authorizerId
cors: true
But the error I am receiving is
An error occurred: ApiGatewayMethodApiPrivateGet - Invalid authorizer ID specified. Setting the authorization type to CUSTOM or COGNITO_USER_POOLS requires a valid authorizer. (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException
Not sure what I am missing here.