AWS SAM-SAM的lambda和api资源之间的多级循环依赖关系

时间:2019-06-01 09:04:25

标签: amazon-cloudformation swagger-2.0 aws-sam

我正在构建一个大型应用程序,该应用程序具有“静态”资源(API,lambda LayerVersions,DynamoDB表等)和“动态”资源(Lambda函数,批处理和SQS队列)。

为了整齐地安排这些内容,我使用SAM的AWS::Serverless::Application类型创建了多个模板文件,其中一些引用了其他模板文件。

在某些“模块”(1级深层嵌套堆栈)中,我有充当lambda代理的lambda,并且与它们集成的API在StaticResourcesModule嵌套堆栈中声明。

但是这些lambda需要AWS::Serverless::Api的{​​{1}}属性to know to which event to respond to

之所以发生循环依赖,是因为首先在needs the Lambda's ARN (or lambda function's name and account number)定义RestApiId的Swagger 2.0模板中知道哪个lambda实现了什么方法(AWS::Serverless::Api的{​​{1}}属性)

当lambda本身需要对API的x-amazon-apigateway-integration属性的引用并且两者都位于不同的堆栈中时,如何将对lambda的引用传递给API?

顶级YAML模板文件如下所示:

uri

RestApiId

Resources:
  StaticResourcesModule:
    Type: AWS::Serverless::Application
    Properties:
      Location: static_resources_module/template.yaml

  DataFetchingModule:
    Type: AWS::Serverless::Application
    Properties:
      Location: datafetching_module/template.yaml
      Parameters:
        MainApiId: !GetAtt StaticResourcesModule.Outputs.MainApiId

datafetching_module/template.yaml

Parameters:
  MainApiId:
    Type: String
    Description: >
      Reference to the main api (AWS::Serverless::Api) resource.

Resources:
  FetchingRequestsHandler:
    Type: AWS::Serverless::Function
    Description: Handles incoming requests [...]
    Properties:
      [...]
      Events:
        ApiPostNewRequestEvent:
          Type: Api
          Properties:
            Method: post
            Path: /fetching/low-priority
            RestApiId: !Ref MainApiId

static_resources_module/template.yaml

Parameters:
    FetchingRequestsHandlerArn:
        Type: String
Resources:
  MainApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      DefinitionUri: api/swagger2_template.yaml

Outputs:
 MainApiId:
   Value: !Ref MainApi
   Description: The Main API's RestApiId which to provide to the Lambda that require an API Event (lambda proxy)

0 个答案:

没有答案