无服务器:从不同的yml部署到相同的api

时间:2017-08-02 07:01:12

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

我有api api.demo

它具有以下结构

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)?

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

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

主要服务中:

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

答案 2 :(得分:-1)

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)

但这些选项都没有缺陷。