如何在无服务器项目中拆分API网关服务

时间:2020-04-18 16:28:26

标签: amazon-web-services serverless-framework

我正在处理CloudFormation每个堆栈200个资源的限制。看来解决方案是将我的服务(serverless.yml文件)拆分为多个文件。我尝试了自动方法和they don't work for me。因此,我正在研究手动的。但是我不知道如何。

这是我的示例文件:

service:                        serverless-test

provider:
  name:                         aws
  runtime:                      nodejs12.x
  endpointType:                 REGIONAL

plugins:
- serverless-aws-alias

functions:
  authorizerFunc:
    handler:                    code.authorizer

  users:
    handler:                    code.users
    events:
      - http:
          path:                 /user
          integration:          lambda
          authorizer:           authorizerFunc
          method:               get
          cors:                 true
          request:
            passThrough:        WHEN_NO_TEMPLATES
            template:
              application/json: '{ "action": "list_users" }'
      - http:
          path:                 /user
          integration:          lambda
          authorizer:           authorizerFunc
          method:               post
          cors:                 true
          request:
            passThrough:        WHEN_NO_TEMPLATES
            template:
              application/json: '{ "action": "create_user", "payload": $input.body }'

  posts:
    handler:                    code.posts
    events:
      - http:
          path:                 /post
          integration:          lambda
          authorizer:           authorizerFunc
          method:               get
          cors:                 true
          request:
            passThrough:        WHEN_NO_TEMPLATES
            template:
              application/json: '{ "action": "list_posts" }'
      - http:
          path:                 /post
          integration:          lambda
          authorizer:           authorizerFunc
          method:               post
          cors:                 true
          request:
            passThrough:        WHEN_NO_TEMPLATES
            template:
              application/json: '{ "action": "create_post", "payload": $input.body }'

有人可以帮我把这个文件分成2个或3个吗?随意以任何您喜欢的方式拆分它(只要生成的文件分别具有较少的资源)。只是JS代码应该保持不变。另外,请密切注意serverless-aws-alias插件。这是我服务的关键部分。当然,其目的是多个文件的部署应与单个文件的部署相同。

1 个答案:

答案 0 :(得分:1)

据我了解,您应该能够通过拆分网关来解决此问题。

建议首先使用共享部件,API网关和授权者的部署创建一个serverless.yml

service: api-gw

provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: eu-west-2

functions:
  authorizerFunc:
    handler: handler.handler

plugins:
  - serverless-aws-alias

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: service-users

provider:
  name: aws
  runtime: nodejs12.x
  region: eu-west-2
  apiGateway:
    restApiId:
      'Fn::ImportValue': MyApiGateway-restApiId
    restApiRootResourceId:
      'Fn::ImportValue': MyApiGateway-rootResourceId
    websocketApiId:
      'Fn::ImportValue': MyApiGateway-websocketApiId

plugins:
  - serverless-aws-alias

functions:
  users:
    handler:                    handler.handler
    events:
      - http:
          path:                 /user
          integration:          lambda
          authorizer:
            arn: authorizerFuncARN
          method:               get
          cors:                 true
          request:
            passThrough:        WHEN_NO_TEMPLATES
            template:
              application/json: '{ "action": "list_users" }'
      - http:
          path:                 /user
          integration:          lambda
          authorizer:           
            arn: authorizerFuncARN
          method:               post
          cors:                 true
          request:
            passThrough:        WHEN_NO_TEMPLATES
            template:
              application/json: '{ "action": "create_user", "payload": $input.body }'

这里也有一个解释:https://serverless.com/framework/docs/providers/aws/events/apigateway#easiest-and-cicd-friendly-example-of-using-shared-api-gateway-and-api-resources

您还应该考虑将授权者信息导出到某种内容中,以便以后在其他无服务器Yaml中重新使用,也许是ssm?

相关问题