# Multiple services under directory

**URL:** <https://forum.serverless.com/t/multiple-services-under-directory/3803>\
**Category:** Serverless Framework\
**Created:** [February 19, 2018, 1:29pm UTC](https://forum.serverless.com/t/multiple-services-under-directory/3803 "2018-02-19T13:29:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![superandrew](https://avatars.discourse-cdn.com/v4/letter/s/a88e57/32.png) [@superandrew](https://forum.serverless.com/u/superandrew)\
**Post date:** [February 19, 2018, 1:29pm UTC](https://forum.serverless.com/t/multiple-services-under-directory/3803/1 "2018-02-19T13:29:17Z")

</div>

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:

```
provider: 
      apiGateway:
        restApiId: xxxxxxxxxx 
        restApiRootResourceId: xxxxxxxxxx
        restApiResources:
        /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

---

<div class="post-metadata">

**Author:** ![genestd](https://avatars.discourse-cdn.com/v4/letter/g/a587f6/32.png) [@genestd](https://forum.serverless.com/u/genestd)\
**Post date:** [February 20, 2018, 6:21pm UTC](https://forum.serverless.com/t/multiple-services-under-directory/3803/2 "2018-02-20T18:21:15Z")

</div>

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.

```
resources:
  Outputs:
    BamGatewayId:
      Description: Id of the auto-generated API Gateway resource
      Value:
        Fn::Join:
          - ''
          - - Ref: ApiGatewayRestApi
      Export:
        Name: bamgateway

```

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

---

<div class="post-metadata">

**Author:** ![Vadorequest-SL](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/vadorequest-sl/32/1630_2.png) [@Vadorequest-SL](https://forum.serverless.com/u/Vadorequest-SL)\
**Post date:** [February 20, 2018, 8:55pm UTC](https://forum.serverless.com/t/multiple-services-under-directory/3803/3 "2018-02-20T20:55:44Z")

</div>

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.

> **[How to deploy multiple micro-services under one API domain with Serverless](https://www.serverless.com/blog/api-gateway-multiple-services)**
>
> Learn how to use the same domain name for multiple Serverless services with API Gateway base path mappings.

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:

```nohighlight
functions:
  service1.getById:
    handler: service1/main.getById
    events:
      - http:
          method: get
          path: service1/get/:id
  service2.create:
    handler: service2/main.create
    events:
      - 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.

---

<div class="post-metadata">

**Author:** ![genestd](https://avatars.discourse-cdn.com/v4/letter/g/a587f6/32.png) [@genestd](https://forum.serverless.com/u/genestd)\
**Post date:** [February 20, 2018, 9:10pm UTC](https://forum.serverless.com/t/multiple-services-under-directory/3803/4 "2018-02-20T21:10:15Z")

</div>

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 [https://mydomain.com/api/assets](https://mydomain.com/api/assets) -\> cloudfront behavior that routes that traffic to the api gateway for the “assets” service.

---

<div class="post-metadata">

**Author:** ![superandrew](https://avatars.discourse-cdn.com/v4/letter/s/a88e57/32.png) [@superandrew](https://forum.serverless.com/u/superandrew)\
**Post date:** [February 21, 2018, 8:04am UTC](https://forum.serverless.com/t/multiple-services-under-directory/3803/5 "2018-02-21T08:04:48Z")

</div>

> [@Vadorequest-SL](#):
>
> I’m a newbie too, but I can think of 2 ways of doing this.

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.

---

<div class="post-metadata">

**Author:** ![rohitshetty](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rohitshetty/32/1951_2.png) [@rohitshetty](https://forum.serverless.com/u/rohitshetty)\
**Post date:** [May 8, 2018, 9:52am UTC](https://forum.serverless.com/t/multiple-services-under-directory/3803/6 "2018-05-08T09:52:29Z")

</div>

I have updated the docs to explain this. You can find it here: [https://github.com/serverless/serverless/blob/master/docs/providers/aws/events/apigateway.md#share-api-gateway-and-api-resources](https://github.com/serverless/serverless/blob/master/docs/providers/aws/events/apigateway.md#share-api-gateway-and-api-resources)

---

<div class="post-metadata">

**Author:** ![DionisisKav](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@DionisisKav](https://forum.serverless.com/u/DionisisKav)\
**Post date:** [January 8, 2019, 2:21pm UTC](https://forum.serverless.com/t/multiple-services-under-directory/3803/7 "2019-01-08T14:21:20Z")

</div>

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

companies/{companyId}/stories  
companies/{companyId}/campaigns

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?

---

<div class="post-metadata">

**Author:** ![prathamj](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@prathamj](https://forum.serverless.com/u/prathamj)\
**Post date:** [February 21, 2019, 7:49pm UTC](https://forum.serverless.com/t/multiple-services-under-directory/3803/8 "2019-02-21T19:49:36Z")

</div>

Hi,  
I copied the code in this link: [https://github.com/serverless/serverless/blob/master/docs/providers/aws/events/apigateway.md#share-api-gateway-and-api-resources](https://github.com/serverless/serverless/blob/master/docs/providers/aws/events/apigateway.md#share-api-gateway-and-api-resources), but i get different endpoints and restApiId’s. Can anyone check what i could have done wrong? here are my 2 services:

service: notes-app-mono-notes

plugins:

- serverless-webpack
- serverless-offline

custom:  
stage: ${opt:stage, self:provider.stage}

webpack:  
webpackConfig: …/…/webpack.config.js  
includeModules: true

provider:  
name: aws  
runtime: nodejs8.10  
stage: dev  
region: us-east-1

environment:  
tableName:  
${file(…/database/serverless.yml):custom.tableName}

iamRoleStatements:  
- Effect: Allow  
Action:  
- dynamodb:DescribeTable  
- dynamodb:Query  
- dynamodb:Scan  
- dynamodb:GetItem  
- dynamodb:PutItem  
- dynamodb:UpdateItem  
- dynamodb:DeleteItem  
Resource:  
- ‘Fn::ImportValue’: ${self:custom.stage}-NotesTableArn

functions:  
get:  
# Defines an HTTP API endpoint that calls the main function in get.js  
# - path: url path is /notes/{id}  
# - method: GET request  
handler: get.main  
events:  
- http:  
path: notes/{id}  
method: get  
cors: true  
authorizer: aws\_iam

resources:  
Resources:  
MyApiGW:  
Type: AWS::ApiGateway::RestApi  
Properties:  
Name: MyApiGW

Outputs:  
apiGatewayRestApiId:  
Value:  
Ref: MyApiGW  
Export:  
Name: MyApiGateway-restApiId

```
apiGatewayRestApiRootResourceId:
  Value:
    Fn::GetAtt:
      - MyApiGW
      - RootResourceId
  Export:
    Name: MyApiGateway-rootResourceId

```

service: notes-app-mono-billing

plugins:

- serverless-webpack
- serverless-offline

custom:  
stage: {opt:stage, self:provider.stage} webpack: webpackConfig: ../../webpack.config.js includeModules: true environment: {file(env.yml):${self:custom.stage}, file(env.yml):default}

provider:  
name: aws  
runtime: nodejs8.10  
stage: dev  
region: us-east-1

apiGateway:  
restApiId:  
‘Fn::ImportValue’: MyApiGateway-restApiId  
restApiRootResourceId:  
‘Fn::ImportValue’: MyApiGateway-rootResourceId

environment:  
stripeSecretKey: ${self:custom.environment.stripeSecretKey}

functions:

billing:  
handler: billing.main  
events:  
- http:  
path: billing  
method: post  
cors: true  
authorizer: aws\_iam

I noticed in cloudfront that my notes service has different service endpoint and apiGatewayRestApiId. Is this supposed to happen?  
Service endpoint: [https://exttceymxb.execute-api.us-east-1.amazonaws.com/dev](https://exttceymxb.execute-api.us-east-1.amazonaws.com/dev)  
apiGatewayRestApiId: e10zsltayj

Thank you

---

<div class="post-metadata">

**Author:** ![Gio](https://avatars.discourse-cdn.com/v4/letter/g/9dc877/32.png) [@Gio](https://forum.serverless.com/u/Gio)\
**Post date:** [January 18, 2022, 9:55pm UTC](https://forum.serverless.com/t/multiple-services-under-directory/3803/9 "2022-01-18T21:55:25Z")

</div>

Hey Dionisis.

I am exactly trying to get a configuration like the one you’re mentioning but failing to get these paths to deploy:  
Having something like:  
companies/{companyId}  
companies/{companyId}/stories  
companies/{companyId}/campaigns

Do you have a working example that could get me started with? I’m failing to get the functions for “stories” and “campaigns” to work.
