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

标签 aws-lambda aws-cloudformation 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

最佳答案

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

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

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

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

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

关于aws-lambda - AWS Cloudformation SAM 文件 - 如何拆分为许多较小的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63705666/

相关文章:

amazon-web-services - 如何获取cloudformation中现有vpc子网的子网ID

amazon-web-services - 如何在AWS SAM模板中使用If条件?

aws-lambda - CloudFormation 创建的 Lambda 不创建 LogStream/Logs

amazon-s3 - 如何引用Cloud Formation中创建的S3存储桶

python-2.7 - AWS lambda (python2.7) : Adding matplotlib as a layer

amazon-dynamodb - 全局二级索引 `ProvisionedThroughput` 属性的无服务器 DynamoDB 资源定义错误

amazon-web-services - 如何将无服务器应用程序模型 (SAM) 模板转换为 Cloudformation?

aws-step-functions - 如何使用文件中定义的状态机在本地执行 AWS Step Functions?

amazon-web-services - 获取现有 lambda 的 AWS cloudformation

aws-lambda - 如何配置我的无服务器 YML 以使用我的 API 网关授权方?