DD Jin
DD Jin

Reputation: 355

AWS CDK resource create sequence?

I'm using the AWS CDK. When I deploy, according to CloudFormation Events in the AWS console, resources(CfnTrigger) in createWorkflow are initialized before createDDBCrawler and createS3Crawler. Which is causing create_failed(Entity not found) since createWorkflow depends on resources in these two.

So I am wondering:

  1. AWS CDK resource generating sequence (since I see no async or promise in functions, so TypeScript is handling the code in sequence. Then it's a CDK/CloudFormation behavior?)

  2. How to avoid this or arrange resource create sequence, except creating two stack.

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

Upvotes: 5

Views: 3070

Answers (1)

user16442705
user16442705

Reputation:

You can cause a construct to become dependent on another construct by calling addDependency on the construct's node property, like this:

// 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 is a practical example.


Probably, you'd want to save the return value of createS3Crawler to a variable, and then pass that variable as an argument to createWorkflow. Then, createWorkflow will call .node.addDependency(createS3Crawler) on each construct that it creates internally which is dependent on the S3 crawler.

Upvotes: 5

Related Questions