Search code examples
amazon-web-servicesamazon-s3aws-cloudformationaws-cdk

Make Quicksight resource depend on an s3 bucket creation in AWS CDK


I got a s3 bucket where that uploads a manifest file on creation in CDK.

This manifest file is then use by an Dataset in Quicksight. But my CDK deployment fails because the manifest file in S3 can't be found by QuickSight. So I want to add a dependsOn for the Quicksight resource.

const quicksightBucket = new s3.Bucket(this, "userS3Bucket", {
            bucketName: "quicksight-bucket-user",
            blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
            versioned: true,
            removalPolicy: cdk.RemovalPolicy.DESTROY,
            autoDeleteObjects: true,
        })

        const bucketDeployment = new s3deploy.BucketDeployment(
            this,
            "bucketDeployment",
            {
                destinationBucket: quicksightBucket,
                sources: [
                    s3deploy.Source.asset("/Users/user/Downloads/housing"),
                ],
            }
        )

                const quicksightDatasource = new quicksight.CfnDataSource(
            this,
            "quicksight-datasource",
            {
                name: "quicksightdatasource",
                awsAccountId: "123123",
                dataSourceId: "7217623409123897423687",
                type: "S3",
                dataSourceParameters: {
                    s3Parameters: {
                        manifestFileLocation: {
                            bucket: quicksightBucket.bucketName,
                            key: "manifest.json",
                        },
                    },
                },
            }
        )

        quicksightDatasource.addDependsOn(bucketDeployment)

I'm getting an error like: Argument of type 'Bucket' is not assignable to parameter of type 'CfnResource'.


Solution

  • To add a dependency on the Bucket itself:

    quicksightDatasource.addDependency(
        quicksightBucket.node.defaultChild as s3.CfnBucket
    );
    

    That's probably not what you want, though. That ensures the bucket exists before the QuickSight resource is created. It doesn't ensure your manifest.json data are in the bucket. To do that, instead add a dependency on the Custom Resource that's deployed by the s3deploy.BucketDeployment:

    quicksightDatasource.addDependency(
        bucketDeployment.node.tryFindChild("CustomResource")?.node.defaultChild as CfnCustomResource
    );