当同时使用cfn模板和使用AWS CDK创建的管道时,如何使用CodePipeline部署CloudFormation堆栈? [英] How to deploy a CloudFormation stack using CodePipeline when both, the cfn template and the pipeline where created using the AWS CDK?

查看:78
本文介绍了当同时使用cfn模板和使用AWS CDK创建的管道时,如何使用CodePipeline部署CloudFormation堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using the AWS CDK I created a simple stack with an auto scaling group, and also define launch configuration resource to execute some powershell scripts during the ec2 instance creation. The scripts are located in the same cdk typescript project and I use and aws-s3-asset construct to upload the scripts directory. When the CDK synthtize the template it creates 3 CloudFormation parameters with autogenerated names to reference the S3 bucket of the assets. At this point every thing works perfect, I execute the cdk deploy StackWithAutoScalingGroup command, the cdk automatically populates the value of CloudFormation parameters and deploys the stack.

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

I decided to implement a CodePipeline stack(StackWithTheCodePipline) to deploy the StackWithAutoScalingGroup, it fetches the code from a CodeCommit repository the executes code build to synthesize the template and as final stage it is a CodeDeploy CloudFormation action used to deploy the stack. This final step is failing because the CloudFormation params where not provided by the pipeline.

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

Im looking for a way to access the s3 Assets bucket created in the StackWithAutoScalingGroup from the StackWithTheCodePipline in order to provide the required CloudFormation params

任何帮助将不胜感激

StackWithAutoScalingGroup.ts

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(在合成堆栈之后创建的参数)

StackWithAutoScalingGroup.template.json (Parameters created afters the stack is Synthesized)

"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

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属性以设置所需的参数,但是就像名称是自动生成的一样,我无法找到一种方法来知道参数,而模板不会期望这些参数的值.

The action provides the parameterOverrides property to set the required parameters but like the names was autogenerated I'm not able to find a way to know the parameters that the template expect neither the value for the parameters.

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

What I'm expect is a way to know the generated param names and also a way to reference the s3 assets bucket to provide the value for the parameters.

       {
          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,
            }),
          ],
        },

推荐答案

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

I'm not entirely sure of how things are setup. I believe you may be able to do something like

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存储桶分配给变量,然后在以后引用该变量.

Those are very much pseudo code. In essence, you can assign the S3 bucket to a variable and then reference that variable later.

这篇关于当同时使用cfn模板和使用AWS CDK创建的管道时,如何使用CodePipeline部署CloudFormation堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆