【问题标题】:AWS SAM : Nested Stacks, Referring to API gateway from the Root stackAWS SAM:嵌套堆栈,从根堆栈引用 API 网关
【发布时间】:2020-04-14 02:35:20
【问题描述】:

我想将我的 SAM 应用程序拆分为多个部分。

我想在根堆栈中创建一个 API ( AWS::Serverless::Api )。

我正在我的子堆栈中创建 lambda 函数,我想在其中将 API 的引用从根堆栈提供给 API 事件。

这可行吗?没有找到从根栈访问API到子栈的好例子?

我尝试使用以下模板 -

parenttemplateapi:
    Type: AWS::Serverless::Application
    Properties:
      Location:
        ApplicationId: arn:aws:serverlessrepo:us-east-1:account_id:applications/parent-template
        SemanticVersion: 1.0.0



HelloWorldFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python2.7
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Method: get
            Path: /hello
            RestApiId: !GetAtt parenttemplateapi.Outputs.ServerlessGW-restApiId

当我尝试部署此模板时,出现以下错误 -

错误:无法为堆栈创建变更集:子模板,例如: Waiter ChangeSetCreateComplete failed: Waiter 遇到终端 故障状态 状态:FAILED。原因:转型 AWS::Serverless-2016-10-31 失败:内部转换失败。

任何指示/建议?

我指的是下面的链接-

https://aws.amazon.com/blogs/compute/announcing-nested-applications-for-aws-sam-and-the-aws-serverless-application-repository/

这里可以使用来自根堆栈的 API Gateway Id (AWS::ApiGateway::RestApi) 吗?

https://dev.to/grahamcox82/building-a-larger-serverless-application-part-3-modular-monorepos-3mon

这可以使用无服务器框架实现吗?

【问题讨论】:

  • 您在其他地方找到解决方案了吗?
  • 这应该是可能的。但是,我想知道父模板的外观以及它是否包含名为 ServerlessGW-restApiId 的输出。
  • @MartinLöper 我想知道你和我是否曾经一起开发过 Visa 令牌服务钱包应用程序! :) 如果是这样,很高兴在 Digital Wild West 与您相遇!
  • @lopezdp 我不记得了。但是很高兴在数字狂野西部见到你:D
  • 您找到解决方案了吗?通过使用 SAM,而不是无服务器框架?

标签: amazon-web-services amazon-cloudformation aws-api-gateway serverless-framework aws-sam


【解决方案1】:

这里需要注意的是,我不使用 AWS SAM,因为我只使用 serverless.yml ServerlessFramework 文件来声明要部署的资源。

在我的ROOT API 中,我声明了我的子 lambda 依赖的一系列输出,以便我的所有端点都可以具有相同的 Rest API ID。这个输出声明嵌套在我的resources 声明下,在我的serverless.yml 文件中,如下所示:

resources:

  Resources:
    ...
    stuff here that you may need
    ...

  # API Gateway Cross Stack Reference Exports!!!!!
  # Outputs that other services will depend on!!!
  Outputs:
    ApiGatewayRestApiId:
      Value:
        Ref: ApiGatewayRestApi
      Export:
        Name: ${self:custom.stage}-ApiGatewayRestApiId

    ApiGatewayRestApiRootResourceId:
      Value:
        Fn::GetAtt:
          - ApiGatewayRestApi
          - RootResourceId
      Export:
        Name: ${self:custom.stage}-ApiGatewayRestApiRootResourceId

实现此功能后,您需要在 serverless.yml 文件的 provider 部分中的子 lambda 中导入对此父 API 资源的引用。



  # Cross-Stack Reference for sharing of API Gateway URI
  # created with accounting-api
  apiGateway:
    restApiId: ${cf:ROOT_API_NAME_HERE-dev.ApiGatewayRestApiId}
    #"Fn::ImportValue": ${self:custom.stage}-ApiGatewayRestApiId
    restApiRootResourceId:
      ${cf:ROOT_API_NAME_HERE-dev.ApiGatewayRestApiRootResourceId}
      #"Fn::ImportValue": ${self:custom.stage}-ApiGatewayRestApiRootResourceId

您必须注意您需要从父 API 的 CloudFormation 堆栈导入的 restApiId。您可以通过进入父 API 中的 CloudFormation 堆栈来查看输出;单击输出并根据需要找到您的 restApiId 值。

还有另一种方法允许您使用 Fn::ImportValue 导入值,您可以查看此处显示的示例:

https://github.com/serverless/examples/blob/master/aws-node-shared-gateway/users/serverless.yml

我在上面粘贴给你的代码中注释掉了这个方法,供你参考。

【讨论】:

    猜你喜欢
    • 2018-06-21
    • 2021-05-04
    • 2018-03-17
    • 2020-10-31
    • 2022-01-19
    • 2021-08-14
    • 2019-01-12
    • 2020-10-31
    • 2017-05-07
    相关资源
    最近更新 更多