使用 CloudFormationCreateReplaceChangeSetAction 处理 yaml 文件时如何从配置文件添加信息?

How can I add information from a config file when processing a yaml-file with CloudFormationCreateReplaceChangeSetAction?

我有一个包含源阶段、构建阶段和自变异阶段的管道。我正在尝试获取现有的 yaml 文件并准备与其关联的项目以进行部署。 yaml 文件使用配置文件进行生产,使用不同的配置文件进行测试,这就是下面的代码产生以下错误的原因:Parameters: [DatabaseNamespace, SecretsKmsKey] must have values (Service: AmazonCloudFormation; Status Code: 400; Error Code: ValidationError;

我一直在查看文档 found here,如果我可以使用其中一个可用的 props 从配置文件传递参数,我想我已经接近解决这个问题了。

管道第四阶段:

    pipeline.addStage({
      stageName: 'Test_Deploy',
      actions: [
        new CloudFormationCreateReplaceChangeSetAction({
          actionName: 'PrepareChanges',
          stackName: 'my-stack',
          changeSetName: 'StagedChangeSet',
          adminPermissions: true,
          templatePath: sourceOutput.atPath('cloudformation/cf-test.yaml'),
          runOrder: 1
        })
      ]
    })

配置文件:

AppStackName=my-stack
AppDeployBucket=deploy-bucket
DatabaseNamespace=cf-test-database
SecretsKmsKey=secrets-kms-key

cf-test.yaml:

Parameters:
  DatabaseNamespace:
    Type: String
    Description: "DynamoDB tables namespace"

Globals:
  Function:
    Runtime: nodejs14.x
    MemorySize: 512
    Timeout: 60
    Environment:
      Variables:
        MY_DATABASE_NS: !Ref DatabaseNamespace

Resources:
  DynamoDbAccessPolicy:
    Type: AWS::IAM::ManagedPolicy
    Properties:
      Description: Permissions to access application dynamodb tables
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Action:
              - "dynamodb:*Item"
              - "dynamodb:Query"
            Resource:
              - !Sub "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${DatabaseNamespace}.*"

这个问题的解决方案是使用 parameter Overrides? 道具 (documentation found here)。

流水线阶段:

    pipeline.addStage({
      stageName: 'Test_Deploy',
      actions: [
        new CloudFormationCreateReplaceChangeSetAction({
          actionName: 'PrepareChanges',
          stackName: 'my-stack',
          changeSetName: 'StagedChangeSet',
          adminPermissions: true,
          templatePath: sourceOutput.atPath('cloudformation/cf-test.yaml'),
          parameterOverrides: {
            DatabaseNamespace: 'cf-test-database',
            SecretsKmsKey: 'secrets-kms-key'
          },
          runOrder: 1
        })
      ]
    })