AWS CDK 资源创建顺序?

时间:2021-07-30 05:51:24

标签: amazon-cloudformation aws-cdk

我使用的是 AWS CDK。当我部署时,根据 AWS 控制台中的 CloudFormation Events,resources(CfnTrigger) 中的 createWorkflowcreateDDBCrawlercreateS3Crawler 之前被初始化。这是导致 create_failed(Entity not found) 的原因,因为 createWorkflow 取决于这两个中的资源。

所以我想知道:

  1. AWS CDK 资源生成序列(因为我在函数中没有看到 asyncpromise,所以 TypeScript 正在按顺序处理代码。那么这是 CDK/CloudFormation 行为?)

  2. 如何避免这种情况或安排资源创建顺序,除了创建两个堆栈。

export class QuicksightGlue extends Construct {

    constructor(scope: Construct, name: string, props: QuicksightGlueProps) {
        super(scope, name);
        this.createGlueDb(props.glueDb);

        for (let zone of zones) {

            const ddbCrawler = this.createDDBCrawler(...);

            const etlJob = this.createEtlJob(...);

            // crawler for processed data
            this.createS3Crawler(...);  

            // create workflow that use crawler
            this.createWorkflow(...);      
        }
    }
private createS3Crawler(...) {
        return new glue.CfnCrawler(this, 's3Crawler_' + zone, {
            name: 's3Crawler_' + zone,
            databaseName: glueDb,
            role: roleArn,
            targets: {
                s3Targets: [{ path: s3 }]
            }
        });
    }
private createWorkflow(...) {
        const extracDdbWorkflow = new glue.CfnWorkflow(this, `ExtractDdb_` + zone, {
            name: `udhcpExtractDdb_` + zone.toLowerCase(),
            description: "Workflow to extract and process data from DDB"
        });

        const scheduledTriggerDdbCrawler = new glue.CfnTrigger(this, 'DdbTrigger_' + zone, {
            workflowName: extracDdbWorkflow.name,
            type: "SCHEDULED",
            schedule: scheduleCronExpression, //"cron(0 * * * ? *)",
            description: "Trigger to start the workflow every hour to update ddb data",
            actions: [{
                crawlerName: ddbCrawler,
                }],
        });
...

1 个答案:

答案 0 :(得分:1)

您可以通过在构造的 addDependency 属性上调用 node 来使构造变得依赖于另一个构造,如下所示:

// Normally these two constructs would be created in parallel
const construct1 = ...;
const construct2 = ...;

// But with this line, construct2 will not be created until construct 1 is
construct2.node.addDependency(construct1);

Here 是一个实际例子。


可能您想将 createS3Crawler 的返回值保存到一个变量中,然后将该变量作为参数传递给 createWorkflow。然后,createWorkflow 将对其内部创建的每个构造调用 .node.addDependency(createS3Crawler),该构造依赖于 S3 爬虫。