AWS Cloudformation SAM 文件 - 如何拆分为许多较小的文件?

Mic*_*ski 1 aws-cloudformation aws-lambda aws-sam

我正在创建定义我们的产品环境的模板。在我们的系统中,我们将使用许多 Lambda,它会生成巨大的 Cloudformation 模板文件,其中包含许多(甚至是大量的)条目,如下所示。可以将一个模板文件拆分为多个单独的文件(例如,一个文件用于一项功能,或者至少一个文件用于一项服务)。我知道有子堆栈机制,但在子堆栈中我无法将函数定义存储在本地文件中(我只能给出模板 URL),并且我不确定是否可以将参数传递给子堆栈。正如下面的示例所示,有许多参数和对其他资源的引用。

  APILambadFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ../lambda_functions/
      Handler: getUserInfo.lambda_handler
      FunctionName: !Sub ${CreatorUsername}-getUserInfo
      Runtime: python3.7
      VpcConfig:
        SecurityGroupIds:
          - !Ref SecurityGroupLamda
        SubnetIds:
          - !Ref PrivateSubnet1
          - !Ref PrivateSubnet2
      Role:
        Fn::GetAtt: [ RoleLamdaRestAPI, Arn ]   # Rola dla wszystkich Lamd restowych
      Environment:
        Variables:
          DB_HOST: !GetAtt 'PostgresDB.Endpoint.Address'
          DB_PORT: !GetAtt 'PostgresDB.Endpoint.Port'
          DB_NAME: !Sub '{{resolve:ssm:/${CreatorUsername}/${EnvType}/PostgresSQL/DBName:1}}'
          DB_USERNAME: !Sub '{{resolve:ssm:/${CreatorUsername}/${EnvType}/PostgresSQL/Username:1}}'
          CREATOR_USERNAME: !Ref CreatorUsername
          ENV_TYPE: !Ref EnvType
      Events:
        GetUserInfo:
          Type: Api
          Properties:
            Path: /user
            Method: get
            RestApiId: !Ref ApiGatewayApi
Run Code Online (Sandbox Code Playgroud)

Rob*_*dey 6

您可以使用 CloudFormation 的“嵌套堆栈”功能。

在“主”template.yaml 中,您定义资源如下:

Resources:
  NestedStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: ./any/directory/infrastructure.yml
      Parameters:
        Environment: Staging
Run Code Online (Sandbox Code Playgroud)

您必须在此模板上运行aws cloudformation package( docs ) 才能使用本地路径。

在这里您可以了解有关嵌套堆栈的更多信息。