I am looking to share an authorizer between different HTTP API services with Serverless. I have seen different links which explain about splitting different endpoints/services into separate holders with their own serverless.yml files, but I cannot find information on sharing an authorizer between these.
I am using a basic HTTP API example (not a REST API setup) like this:
org: orgexample
app: app-example
service: notes-api
plugins:
- serverless-bundle
provider:
name: aws
runtime: nodejs12.x
region: eu-west-2
environment:
DOMAIN_SUFFIX: notes-api
httpApi:
authorizers:
serviceAuthorizer:
identitySource: $request.header.Authorization
issuerUrl:
Fn::Join:
- ""
- - "https://cognito-idp."
- "${opt:region, self:provider.region}"
- ".amazonaws.com/"
- Ref: serviceUserPool
audience:
- Ref: serviceUserPoolClient
functions:
getProfileInfo:
handler: main.get
events:
- httpApi:
method: GET
path: /user/profile
authorizer: serviceAuthorizer
createProfileInfo:
handler: main.post
events:
- httpApi:
method: POST
path: /user/profile
authorizer: serviceAuthorizer
resources:
Resources:
HttpApi:
DependsOn: serviceUserPool
serviceUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: ${self:service}-user-pool-${opt:stage, self:provider.stage}
UsernameAttributes:
- email
AutoVerifiedAttributes:
- email
serviceUserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
ClientName: ${self:service}-user-pool-client-${opt:stage, self:provider.stage}
AllowedOAuthFlows:
- implicit
AllowedOAuthFlowsUserPoolClient: true
AllowedOAuthScopes:
- phone
- email
- openid
- profile
- aws.cognito.signin.user.admin
UserPoolId:
Ref: serviceUserPool
CallbackURLs:
- https://localhost:3000
ExplicitAuthFlows:
- ALLOW_USER_SRP_AUTH
- ALLOW_REFRESH_TOKEN_AUTH
GenerateSecret: false
SupportedIdentityProviders:
- COGNITO
serviceUserPoolDomain:
Type: AWS::Cognito::UserPoolDomain
Properties:
UserPoolId:
Ref: serviceUserPool
Domain: ${self:service}-user-pool-domain-${opt:stage, self:provider.stage}-${self:provider.environment.DOMAIN_SUFFIX}
This will create the HTTP API, API Gateway and wrap it in a Cognito authorizer. I would like to set up a second service that uses the same authorizer.
I have seen similar questions, but none relating to HTTP APIs and sharing a Cognito Authorizer. Useful links:
https://seed.run/blog/how-to-structure-a-real-world-monorepo-serverless-app.html.
https://github.com/seed-run/serverless-template-monorepo.