Same base path for different api gateway

Hi everyone, I’m facing this scenario. I have two api gateways in different repositories. In one api gateway I process everything that came under /api and on the other one I want to process /api/users. Is there any way to achieve this?

Hi @lucholafrazia1 imagine this scenario:
you create you first service in serverless.yml
service 1:
provider:
name: aws
runtime: nodejs14.x
region: us-east-1
timeout: 20
functions:
add-role-admin:
handler: backend/src/functions/add-role-admin/add-role-admin.add_role_admin
name: ${sls:stage}-add-role-admin
description: add-role-admin
events:
- http:
cors: true
method: post
path: backend-admin/add-role-admin

in this first function, you create the apigateway

in the second function (different repository and different serverless.yml) you reference the rest api ID and the rest api resource ID from the first service

service 2:

provider:
name: aws
runtime: nodejs14.x
region: us-east-1
apiGateway:
restApiId: ${ssm:/XXXXXXX/secrets/RESTAPIID}
restApiRootResourceId: ${ssm:/XXXXXXX/secrets/RESTAPIROOTRESOURCEID}
description: xxxx xxxx xxxx
functions:
create-admin:
handler: backend/src/functions/create-admin/create-admin.create_admin
name: ${sls:stage}-create-admin
description: create-admin
events:
- http:
cors: true
method: post
path: backend-admin/create-admin

when you reference the element “apiGateway” in provider section and pass the rest api ID and and rest root resorce ID, every function than you create in the serverless.yml are attached to the first apigateway

I hope this info help you

1 Like

Hi @sergioal12. Thanks for the reply. That’s amazing. Could this be apply to function level?

Let’s say that I have several events in my serverless.yml.

events:
- http:
  cors: true
  method: post
  path: backend-admin/create-admin

- http:
  cors: true
  method: post
  path: backend-admin/update-admin

Could I set that backend-admin/update-admin be handle by another Api Gateway?

Thanks a lot my friend

I never test this approach:

functions:
  create-admin:
    handler: backend/src/functions/create-admin/create-admin.create_admin
    name: ${sls:stage}-add-role-admin
    description: add-role-admin
    events:
      - http:
          cors: true
          method: post
          path: backend-admin/create-admin

  update-admin:
    handler: backend/src/functions/update-admin/update-admin.update_admin
    name: ${sls:stage}-update-admin
    description: update-admin
    events:
      - http:
          cors: true
          method: post
          path: backend-admin/update-admin
    apiGateway:
         restApiId: ${ssm:/XXXXXXX/secrets/RESTAPIID}
         restApiRootResourceId: ${ssm:/XXXXXXX/secrets/RESTAPIROOTRESOURCEID}
         description: xxxx xxxx xxxx

apiGateway element is at same level events element

1 Like

I’ll give a try and let you know if it works. Thanks a lot

1 Like

My friend! It worked like a charm! You’re the best @sergioal12

1 Like