【问题标题】:Serverless: Deploy to same api from different yml无服务器:从不同的 yml 部署到相同的 api
【发布时间】:2018-01-09 06:09:36
【问题描述】:

我有api@​​987654321@

结构如下

api.demo
   /service1:
     GET /test1
     GET /test2
     ...
  /service2
     GET /test1
     GET /test2
     ...

我有两个单独的文件夹service1, service2 每个都有自己的 serverless.yml 文件 当我部署它时,它会在 api 网关中创建两个单独的 api,如下所示

api.demo(id1)
   /service1:
     GET /test1
     GET /test2
     ...
api.demo(id2)
   /service2
     GET /test1
     GET /test2

如何更新 service2 以使用相同的 api 端点 api.demo(id1)?

我怎样才能做到这一点? 任何指针都会有所帮助。

【问题讨论】:

    标签: node.js aws-api-gateway amazon-cloudformation serverless-framework


    【解决方案1】:

    只需使用自定义域名:http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html

    您可以将基本路径映射到 API,因此您可以将 /service1 映射到第一个 API 并将 /service2 映射到第二个 API。客户将访问自定义域名,而不是我们提供的默认 execute-api 端点。

    【讨论】:

      【解决方案2】:

      在主要服务中:

      resources:
        Outputs:
          ApiGatewayRestApiId:
            Value:
              Ref: ApiGatewayRestApi
            Export:
              Name: prod-ApiGatewayRestApiId
      
          ApiGatewayRestApiRootResourceId:
            Value:
               Fn::GetAtt:
                - ApiGatewayRestApi
                - RootResourceId 
            Export:
              Name: prod-ApiGatewayRestApiRootResourceId
      

      在二级服务中:

      provider:
        name: aws
        runtime: nodejs8.10
        stage: dev
        region: us-east-1
      
        apiGateway:
          restApiId:
            'Fn::ImportValue': prod-ApiGatewayRestApiId
          restApiRootResourceId:
            'Fn::ImportValue': prod-ApiGatewayRestApiRootResourceId
      

      来源:https://serverless-stack.com/chapters/api-gateway-domains-across-services.html

      【讨论】:

        【解决方案3】:

        https://serverless.com/framework/docs/providers/aws/guide/services/

        注意:目前,每个服务都会在 AWS API Gateway 上创建一个单独的 REST API。由于 AWS API Gateway 的限制,每个 REST API 只能有一个自定义域。如果您计划制作大型 REST API,请注意此限制。此外,修复工作正在进行中,是当务之急。

        正在进行的修复可以是tracked here

        relevant forum thread 有一些建议。可能最简单的方法是拥有一个额外的 API 网关来委托给真正的网关, 要么通过

        • HTTP 重定向
        • 代理到其他 API
        • 复制 api(直接链接到实际 API 网关后面的任何 lambda)

        不过,这些选项都没有缺陷。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-07-26
          • 2020-04-10
          • 1970-01-01
          • 2021-06-12
          • 1970-01-01
          • 2019-03-08
          • 2017-04-23
          • 1970-01-01
          相关资源
          最近更新 更多