4

我正在尝试 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. 但是,它似乎并没有等待堆栈完成。

这是我当前的输出:

{
   "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 或某种等待堆栈完成的方式?

4

1 回答 1

7

如果您尝试引用将生成的 CloudFront 分配 ID 等“动态”内容,我可能会尝试使用 2 个不同的堆栈,并且其中一个依赖于另一个。

我不确定我是否正确理解了您的用例。但也许可以查看包含如何参数化某些事物并跨堆栈传递信息的核心包自述文件。

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 手动或在 CI/CD 管道中查询 AWS api如果我们之后要自动化更多的事情。

示例 2:使用前面的示例,我们将定义一个名为 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. '
});
 }
}

After the stack is created, in the pipeline/cli, use the following command to get the value of the variable:

aws cloudformation describe-stacks --stack-name CloudFrontStack --query "Stacks[0].Outputs[?OutputKey=='CloudFront-DistributionId'].OutputValue"

This will produce the Distribution ID that was created after the stack built.

于 2019-08-16T11:51:05.823 回答