在同时使用cfn模板和使用AWS CDK创建的管道的情况下,如何使用CodePipeline部署CloudFormation堆栈?

时间:2019-08-18 17:50:01

标签: typescript amazon-cloudformation aws-codepipeline aws-cdk

使用AWS CDK,我创建了一个具有自动伸缩组的简单堆栈,并且还定义了启动配置资源,以在ec2实例创建期间执行一些powershell脚本。这些脚本位于相同的cdk typescript项目中,我使用and aws-s3-asset构造上传脚本目录。当CDK合成模板时,它会使用自动生成的名称创建3个CloudFormation参数,以引用资产的S3存储桶。至此,一切正常,我执行了cdk deploy StackWithAutoScalingGroup命令,该cdk自动填充CloudFormation参数的值并部署堆栈。

我决定实现一个CodePipeline堆栈(StackWithTheCodePipline)来部署StackWithAutoScalingGroup,它从CodeCommit存储库中获取代码,执行代码生成以合成模板,最后,它是一个用于部署堆栈的CodeDeploy CloudFormation操作。最后一步失败了,因为管道未提供CloudFormation参数。

我正在寻找一种从StackWithTheCodePipline访问在StackWithAutoScalingGroup中创建的s3资产存储桶的方法,以提供所需的CloudFormation参数

任何帮助将不胜感激

StackWithAutoScalingGroup.ts

const captivaServer = new AutoScalingGroupStandard(this, 'CaptivaServer', {
      vpc: props.vpc,
      instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),
      machineImage: new ec2.WindowsImage(ec2.WindowsVersion.WINDOWS_SERVER_2019_ENGLISH_FULL_BASE),
    });

const scripts = new assets.Asset(this, 'Scripts', {
path: path.join('cfninit', 'scripts'),
readers: [
captivaServer.instanceRole,
]
});

StackWithAutoScalingGroup.template.json(在合成堆栈之后创建的参数)

"Parameters": {
    "SsmParameterValueawsserviceamiwindowslatestWindowsServer2019EnglishFullBaseC96584B6F00A464EAD1953AFF4B05118Parameter": {
      "Type": "AWS::SSM::Parameter::Value<String>",
      "Default": "/aws/service/ami-windows-latest/Windows_Server-2019-English-Full-Base"
    },
    "ScriptsS3Bucket1E273C2D": {
      "Type": "String",
      "Description": "S3 bucket for asset \"CdkCaptivaStack/Scripts\""
    },
    "ScriptsS3VersionKey0B5B668F": {
      "Type": "String",
      "Description": "S3 key for asset version \"CdkCaptivaStack/Scripts\""
    },
    "ScriptsArtifactHashC07F896B": {
      "Type": "String",
      "Description": "Artifact hash for asset \"CdkCaptivaStack/Scripts\""
    }
  }

StackWithTheCodePipline.ts

    new codepipeline.Pipeline(this, 'Pipeline', {
      stages: [
        {
          stageName: 'Source',
          actions: [
            new codepipeline_actions.CodeCommitSourceAction({
              actionName: 'CodeCommitSource',
              repository: code,
              output: sourceOutput,
            }),
          ],
        },
        {
          stageName: 'Build',
          actions: [
            new codepipeline_actions.CodeBuildAction({
              actionName: 'BuildStack',
              project: buildProject,
              input: sourceOutput,
              outputs: [buildOutput],
            }),
          ],
        },
        {
          stageName: 'DeployToTest',
          actions: [
            new codepipeline_actions.CloudFormationCreateUpdateStackAction({
              actionName: 'DeployStack',
              templatePath: buildOutput.atPath('StackWithAutoScalingGroup.template.json'),
              stackName: 'csg-cdk-captiva',
              //parameterOverrides: props.parameterOverrides,
              adminPermissions: true,
            }),
          ],
        },
      ],
    });

该操作提供parameterOverrides属性以设置所需的参数,但是就像名称是自动生成的一样,我无法找到一种方法来知道参数,而模板不会期望这些参数的值。

我希望这是一种了解生成的参数名称的方法,也是一种引用s3资产存储桶以提供参数值的方法。

       {
          stageName: 'DeployToTest',
          actions: [
            new codepipeline_actions.CloudFormationCreateUpdateStackAction({
              actionName: 'DeployStack',
              templatePath: buildOutput.atPath('StackWithAutoScalingGroup.template.json'),
              stackName: 'csg-cdk-captiva',
              parameterOverrides: {
                   'ScriptsS3Bucket1E273C2D':????,//how I can get the param name and also the values
                   'ScriptsS3VersionKey0B5B668F':???,
               }
              adminPermissions: true,
            }),
          ],
        },

1 个答案:

答案 0 :(得分:0)

我不确定如何设置。我相信您也许可以做类似的事情

let yourS3Bucket = cdk.S3(blah)

parameterOverrides: {
  yourS3Bucket:yourS3Bucket.<your_property>
}

代替

parameterOverrides: {
                   'ScriptsS3Bucket1E273C2D':????,//how I can get the param name and also the values
                   'ScriptsS3VersionKey0B5B668F':???,
               }

这些都是伪代码。本质上,您可以将S3存储桶分配给变量,然后在以后引用该变量。