Chris Paton
Chris Paton

Reputation: 5233

Serverless Framework - Two services under one APIGW endpoint

If I have two services, 'Users' and 'Products', each with several functions with endpoints defined for each one (as any traditional API would), is it possible for them to be organised separately in a code base (for clarity) but once deployed share the same API base URL? For example, consider I have the following structure:

/src
-- /users
---- event.json
---- handler.js
---- serverless.yml
-- /products
---- event.json
---- handler.js
---- serverless.yml

and my src/users/serverless.yml has the following defined:

functions:
  create:
    handler: handler.create
    events:
      - http: POST user

  read:
    handler: handler.read
    events:
      - http: GET user

and my src/products/serverless.yml has basically the same thing, just swap 'user' for 'products'.

Currently, both of those services will be deployed to distinctly different API endpoints, one with a URL https://fghijklmnop.execute-api... and another with a URL https://abcdevwxyz.execute-api....

My question is, would it be possible to have these services be deployed but remain under a single API with a single URL (so both would be served under URL https://abcdevwxyz.execute-api....)?

I'm assuming the answer to be, 'No, because Cloud Formation...', but I thought I would post the question here simply for the sake of discussion and to aid my own understanding of building serverless applications.

I'm aware of using Custom Domains, as per the answer here, but for a quicker development cycle this is not really an ideal solution.

My only solution so far would be to simply create a service called 'api' which would contain all the endpoints my API would need which would simply invoke my other services' Lambda functions directly rather than via previously-configured endpoints. It would be an abstraction layer, really, but add potentially unnecessary layers to my application. Again, curious to see what the community feels on this.

Upvotes: 5

Views: 1137

Answers (5)

Chris Paton
Chris Paton

Reputation: 5233

I came up with my own solution to this problem. I abstracted the integration points of my application so that I have specific Integration services (API, S3, SNS, etc.) which respond to events and then process those events and delegate them to separate microservices. I wrote an article on it, with code examples.

Upvotes: 0

theJasonHall
theJasonHall

Reputation: 136

What I've done with my own code is to pull all code out of the handler.js files and put it inside modules. These modules would be required into the handler.js files and a simple function would then be called.

usersModule.js:

export const doSomething = () => {
    // Do something here.
};

users/handler.js:

import {doSomething} from '../.../usersModule.js';

export const handler = ( event, context, callback ) => {
    doSomething();
    // Do other stuff...
    callback( null, "Success" );
};

This way, you can place the meat of your code wherever you would like to have it, organized in whatever way makes sense to you.

You would, however, still have to have a single API defined. Or use the answer from RyanG-AWS to merge the APIs.


If you'd still like to keep code and API definitions separate, you could create the users API and the products API separately. You would then have another combined API that would call either of these APIs. So this way, you would have a single service with a single base URL that you would call. You can do this with integration type HTTP. I have not tried this, so I don't know how well it would work.

Upvotes: 1

Sigma
Sigma

Reputation: 542

You can use Custom Domain Names : http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html

setup the custom domains name (you'll need a SSL certificate) http://myapi.com/

then map your apis :

http://myapi.com/users
http://myapi.com/products 

Just call your functions like this :

http://myapi.com/users/create
http://myapi.com/users/read
http://myapi.com/products/whaterver

Upvotes: 0

RyanG
RyanG

Reputation: 4152

I can't speak directly to Serverless framework support, but this is certainly possible in API Gateway.

You can maintain multiple Swagger files for each "sub API", and use import?mode=merge to import both definitions into the same API.

See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-import-api.html

Thanks, Ryan

Upvotes: 1

hellomichibye
hellomichibye

Reputation: 4292

You can put multiple functions in one serverless.yml

/src
-- event.json
-- users.handler.js
-- products.handler.js
-- serverless.yml

Upvotes: 1

Related Questions