Serverless framework v1 - multiple resources in one service

前端 未结 3 1874
清歌不尽
清歌不尽 2021-01-20 14:33

I have two resources, games and players, both have crud functions. Are these supposed to be in the same serverless service? I would like to separate them, but how do I then

相关标签:
3条回答
  • 2021-01-20 14:57

    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

    0 讨论(0)
  • 2021-01-20 14:59

    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.

    0 讨论(0)
  • 2021-01-20 15:06

    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
    
    0 讨论(0)
提交回复
热议问题