首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在无服务器项目中拆分API网关服务

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

Stack Overflow用户
提问于 2020-04-19 00:28:26
回答 1查看 340关注 0票数 1

我正在处理每个堆栈最多只能使用200个资源的CloudFormation限制。似乎解决方案是将我的服务(serverless.yml文件)分成多个文件。我尝试过自动化方法和they don't work for me。所以,我正在研究手动的。但我不知道该怎么做。

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

代码语言:javascript
代码运行次数:0
运行
复制
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 }'

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

EN

回答 1

Stack Overflow用户

发布于 2020-04-20 01:22:17

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

我建议首先创建一个部署了共享部件、应用编程接口网关和授权程序的serverless.yml

代码语言:javascript
代码运行次数:0
运行
复制
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

其余部分将根据资源数量进行拆分:

代码语言:javascript
代码运行次数:0
运行
复制
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

您还应该考虑将您的授权器arn导出到以后可以在其他无服务器的yaml中重用的东西,比如ssm?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61292542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档