How to invalidate a Cloudfront Distribution using the Python CDK?

0

This is for a static site served from an s3 bucket.

I have this:

distribution = aws_cloudfront.Distribution.from_distribution_attributes(self,
                                                                                id="ImportedDistribution",
                                                                                domain_name='https://hgfdh.cloudfront.net',
                                                                                distribution_id='hgfdhgfdh')

        #now invalidate the distribution
        distribution.invalidate(
            aws_cloudfront.InvalidationBatch(
                paths=['/*'],
                caller_reference=f'Invalidate {distribution.distribution_id}'
            )
        )

but I get: AttributeError: '<class 'aws_cdk.Resource'>+<class 'aws_cdk.aws_cloudfront._IDistributionProxy'>' object has no attribute 'invalidate'

1 Answer
1

I think this is correct, you do it as part of the BucketDepolyment:

aws_s3_deployment.BucketDeployment(self, "DeployWithInvalidation",
                                           sources=[aws_s3_deployment.Source.asset("./build/fdsf')],
                                           destination_bucket=website_bucket,
                                           distribution=distribution,
                                           distribution_paths=['/*'],
                                           )

https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_s3_deployment/README.html#cloudfront-invalidation

Mick B
answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions