我正在尝试AWS CDK并在尝试执行依赖于堆栈完成的代码块时陷入困境。
这是我当前的代码:
class Application extends cdk.Construct {
constructor(scope: cdk.Construct, id: string) {
super(scope, id);
const webStack = new WebsiteStack(app, `website-stack-${id}`, { stage: id })
const buildStack = new CodeBuildStack(app, `codebuild-stack-${id}`, { stage:id, bucket: webStack.websiteBucket, distribution: webStack.websiteDistribution });
this.generateBuildParameter(id, webStack, buildStack)
}
generateBuildParameter(id: string, webStack: WebsiteStack, buildStack: CodeBuildStack) {
const buildParam = {
projectName: buildStack.buildProject.projectName,
sourceVersion: id,
environmentVariablesOverride: [
{ name: "STAGE", value: id, type: "PLAINTEXT" },
{ name: "WEBSITE_BUCKET", value: webStack.websiteBucket.bucketName, type: "PLAINTEXT" },
{ name: "CLOUDFRONT_DISTRIBUTION_ID", value: webStack.websiteDistribution.distributionId, "type": "PLAINTEXT" }
],
buildspecOverride: "./buildspec.yml"
}
fse.outputJson(`./cdk.out/build-parameters/build-${id}.json`, buildParam, (err: Error) => {
if (err) {
throw err
};
console.log(`build parameter has been created in "../cdk.out/build-parameters/build-${id}.json"`);
})
}
}
我只是试图生成一个依赖于buildStack
的json文件。但是,似乎不是在等待堆栈完成。
这是我当前的输出:
{
"projectName":"${Token[TOKEN.41]}",
"sourceVersion":"master",
"environmentVariablesOverride":[{"name":"STAGE","value":"master","type":"PLAINTEXT"},{"name":"WEBSITE_BUCKET","value":"${Token[TOKEN.17]}","type":"PLAINTEXT"},{"name":"CLOUDFRONT_DISTRIBUTION_ID","value":"${Token[TOKEN.26]}","type":"PLAINTEXT"}],
"buildspecOverride":"./buildspec.yml"
}
AWS CDK是否支持Promise或某种方式来等待堆栈完成?
答案 0 :(得分:1)
如果您尝试引用将要生成的诸如CloudFront分布ID之类的“动态”内容,则我可能会尝试使用2个不同的堆栈,并且其中一个依赖于另一个。
我不确定我是否正确理解了您的用例。但是也许请查看Core软件包自述文件,其中包含如何对某些事物进行参数化以及如何在堆栈之间传递信息。
https://docs.aws.amazon.com/cdk/api/latest/docs/core-readme.html
编辑:您可以执行以下操作:
var s1 = new stackOne();
var s2 = new stackTwo().addDependency(s1);
此博客文章对我有所帮助:https://lanwen.ru/posts/aws-cdk-edge-lambda/
编辑:一些在堆栈之间共享资源的实际示例。 StackA创建一个CloudFront分配(分配的ID是动态的)
StackB需要CloudFront分配ID来设置警报。
// stackA
export class CloudFrontStack extends cdk.Stack {
readonly distribution: cf.CloudFrontWebDistribution;
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
distribution = new cf.CloudFrontWebDistribution(this, 'my-cloud-front-dist', {//props here...} );
}
}
// stack B
export class AlarmStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, cloudfrontDistributionId: string, props?: cdk.StackProps) {
super(scope, id, props);
new alarm.Alarm()// alarm definition, need ID of CF Distribution here.
}
}
//index.ts where everything is linked:
const app = new cdk.App();
const stack1= new CloudFrontStack(app, 'CFStack1');
const stack2= new AlarmStack(app, 'AlarmStack', stack1.distribution.distributionId);
// you can even specify that stack2 cannot be created unless stack1 succeeds.
stack2.addDependency(stack1);
EDIT2 :要使用在构建堆栈之后并在CDK之外创建的资源,我想到的最简单的方法是定义CfnOutputs,然后使用CLI查询AWS api ,可以手动执行,也可以在CI / CD管道中执行(如果之后要自动化)。
Example2:在前面的示例中,我们将定义一个名为CloudFront-DistributioId的输出,并使用CLI进行查询。
// stackA
export class CloudFrontStack extends cdk.Stack {
readonly distribution: cf.CloudFrontWebDistribution;
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
distribution = new cf.CloudFrontWebDistribution(this, 'my-cloud-front-dist', {//props here...} );
// define a cloud formation output so we can query later
new CfnOutput(this, 'CloudFront-DistributionId', {
exportName: 'CloudFront-DistributionId',
value: cloudFrontDistribution.distributionId,
description: 'The dynamic value created by aws of our CloudFront distribution id. '
});
}
}
创建堆栈后,在管道/ cli中,使用以下命令获取变量的值:
aws cloudformation describe-stacks --stack-name CloudFrontStack --query "Stacks[0].Outputs[?OutputKey=='CloudFront-DistributionId'].OutputValue"
这将生成在构建堆栈之后创建的分发ID。