Serverless framework v1 - multiple resources in one service

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 22:23:53

A serverless framework projects deploys a single API Gateway. So if you want it to be in different API Gateways you need separate serverless framework projects.

Depending on the size of the services you are making it can make sense or it might not.

To merge the two API Gateways higher up you can use API Gateway Custom Domains and proxy the requests based on the path to different API Gateways and stages, keeping one single domain for them all.

In your example you would want to keep them in the same serverless framework. I would create two files player.js and game.js in src/controllers to seperate out the logic.

You can setup serverless with the following YAML file

functions:
  player_info:
    handler: src/controllers/player.info
    events:
    - http:
        path: player # path in the url
        method: get
  player_create:
    handler: src/controllers/player.create
    events:
    - http:
        path: player # path in the url
        method: post
  player_delete:
    handler: src/controllers/player.delete
    events:
    - http:
        path: player # path in the url
        method: delete
  game_info:
    handler: src/controllers/game.info
    events:
    - http:
        path: player # path in the url
        method: get
  game_create:
    handler: src/controllers/game.create
    events:
    - http:
        path: player # path in the url
        method: post
  game_delete:
    handler: src/controllers/game.delete
    events:
    - http:
        path: player # path in the url
        method: delete

One way of doing what you want is to use serverless to deploy the lambdas but to manually set API Gateway to link the endpoints to the lambdas.

There is a restriction in serverless stated here: https://serverless.com/framework/docs/providers/aws/guide/services/

Where it states:

Currently, every service will create a separate REST API on AWS API Gateway. Due to a limitation with AWS API Gateway, you can only have a custom domain per one REST API. If you plan on making a large REST API, please make note of this limitation. Also, a fix is in the works and is a top priority.

In our experience, we manage to have Services with different API and a routing object in our clients.

To decide if they should be in the same serverless service, you need to get into Modeling. In our case, we answer this questions:

  1. Are the entities related?
  2. Are the entities and methods going to change at the same rate?
  3. Is a consumer going to consume one set of entities without consuming the other?

When you change games are you going to change players, etc?

This link can help you with that answer: https://martinfowler.com/articles/microservices.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!