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

Amazon web services 如何在无服务器项目中拆分API网关服务,amazon-web-services,serverless-framework,Amazon Web Services,Serverless Framework,我正在处理每个堆栈200个资源的CloudFormation限制。解决方案似乎是将我的服务(serverless.yml文件)拆分为多个文件。我尝试过自动化的方法和方法。所以,我正在研究手动的。但我不知道怎么做 这是我拥有的一个示例文件: service: serverless-test provider: name: aws runtime: nod

我正在处理每个堆栈200个资源的CloudFormation限制。解决方案似乎是将我的服务(serverless.yml文件)拆分为多个文件。我尝试过自动化的方法和方法。所以,我正在研究手动的。但我不知道怎么做

这是我拥有的一个示例文件:

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代码应该保持不变。另外,请密切关注
无服务器aws别名
插件。这是我服务的关键部分。当然,其目的是部署多个文件应与部署单个文件相同。

据我所知,您应该能够通过拆分网关来处理此问题

建议首先创建一个serverless.yml,并部署共享部件、API网关和授权程序

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 }'
这里还有一个解释:


您还应该考虑将您的授权器ARN导出到其他服务器端的AMLLS中,以后再使用,也许SSM?

谢谢。我自己也在研究那个文档。但到目前为止,我一直在努力解决的问题是,上面提到的插件无法使用这种方法。您是否已使用正确创建和使用的别名部署此服务?能否指定哪些插件不起作用?创建的API网关没有指向lambda别名的stage变量,因此,它指向$LATEST。