【问题标题】:AWS Cloudformation SAM file - how to split to many smaller files?AWS Cloudformation SAM 文件 - 如何拆分为许多较小的文件?
【发布时间】:2020-12-21 15:08:24
【问题描述】:

我正在创建定义我们的产品环境的模板。 在我们的系统中,我们将使用许多 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

【问题讨论】:

标签: aws-lambda amazon-cloudformation aws-sam


【解决方案1】:

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

在您的“主”template.yaml 中,您可以按如下方式定义资源:

Resources:
  NestedStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: ./any/directory/infrastructure.yml
      Parameters:
        Environment: Staging

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

Here你可以了解更多关于嵌套堆栈的信息。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-06-26
  • 2021-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 2012-06-06
相关资源
最近更新 更多