How to invalidate objects in with CloudFront using boto and python3? How to invalidate objects in with CloudFront using boto and python3? python-3.x python-3.x

How to invalidate objects in with CloudFront using boto and python3?


The actual error is below:

<Message>The specified distribution does not exist.</Message>

According to your code, you have specified 'foobar35' as your distribution id - it's not correct.

Before trying to invalidate an object, you need to create a distribution. After you create it, you will receive a distribution ID that should be passed as a parameter to your create_invalidation_request method.

For more information, see: Creating or Updating a Web Distribution Using the CloudFront Console.


The answer from Vladimir Mukhin was helpful: He was correct that my distribution ID was incorrect and that I needed to create a distribution. However, I didn't know how to get my distribution ID. I found it by looking through the awscli docs. The answer was not simply RTFM however, since the information I needed was not easy to find. Here's the answer that finally helped me

aws cloudfront list-distributions

With that info, I was able to find the parallels in the Python, Ruby, and Perl (Paws) APIs to get the correct distribution ID without shelling out to the command line. Hope this helps someone.


using boto3

def invalidate(distributionId:str, path:str='/*')->str:  '''    create a cloudfront invalidation    parameters:      distributionId:str: distribution id of the cf distribution      path:str: path to invalidate, can use wildcard eg. "/*" means all path    response:      invalidationId:str: invalidation id  '''  cf = boto3.client('cloudfront')  # Create CloudFront invalidation  res = cf.create_invalidation(      DistributionId=distributionId,      InvalidationBatch={          'Paths': {              'Quantity': 1,              'Items': [                  path              ]          },          'CallerReference': str(time.time()).replace(".", "")      }  )  invalidation_id = res['Invalidation']['Id']  return invalidation_id

use nicHelper

pip install nicHelper

from nicHelper.cloudfront import invalidate# invalidate(<distributionId>, <path>)invalidate("EIOJ239OIUOIU","/*")