Multiple services under directory


Hi, newbie here. I’d like to deploy a serverless architecture with multiple subservices under a single api gateway.

So far I understood there should be a supported configuration which is:

        restApiId: xxxxxxxxxx 
        restApiRootResourceId: xxxxxxxxxx
        /posts: xxxxxxxxxx

But apart from the first series of xxx, I am not able to understand from serverless documentation, what restApiRootResourceId or /posts xxxx values should be supplied.

Can anyone help me understand this? thanks


I don’t have a solution, but I have been trying to figure this out for a couple days. Can someone PLEASE post example YML from 2 services with 1 apiGatewayId?

I have tried using an Output Value and an Export from the main stack resources, and then using that variable in the second stack’s provide. But it always creates 2 separate ApiGateways.

      Description: Id of the auto-generated API Gateway resource
          - ''
          - - Ref: ApiGatewayRestApi
        Name: bamgateway

I can’t seem to find an example of this solution anywhere…


I’m a newbie too, but I can think of 2 ways of doing this.

The first way is to use a custom domain and url rewriting (through baseUrl) for each service. Basically each service will have its own CloudFront, but they’ll all be available though a common custom domain.

For instance, using this model, I can have:

  • https://api.mydomain/service1/get/:id
  • https://api.mydomain/service1/getAll
  • https://api.mydomain/service2/create

The 2nd way would be to only use 1 service but split your code in subdirectories:

    handler: service1/main.getById
      - http:
          method: get
          path: service1/get/:id
    handler: service2/main.create
      - http:
          method: post
          path: service2/create

I haven’t tested the 2nd solution but read about it online a few times. I’d rather go with the 1st solution or I guess the serverless.yml will grow quite a lot if it contains all endpoints of all services.


I’m starting to implement a variation of that first solution now:

I am deploying all of my services to separate API gateways. I have a cloudfront CDN in front of them with each API-G mapped to a behavior.

So -> cloudfront behavior that routes that traffic to the api gateway for the “assets” service.


I think both are brilliant solutions, but there should be a way of doing this using the restApiId, restApiRootResourceId and in general the apiGateway configuration section.


I have updated the docs to explain this. You can find it here:


Hey great documentation. I 've also deploy many microservices under same API GATEWAY.

But i find some one big issue on this approach.
There is the chance that 2 services use the same parent resource /resource so in order to solve this I 've created a seperate serverless.yml that builds all resource in ApiGateway and output the values in the stack value. But thinks getting harder in examples like you have urls


Now you need a value in the stack for both companies and companies/{companyId} resource

While api is getting bigger you either have more resources to create and output or you should find other ways to format your urls

What would you suggest?