Skip to content

Instantly share code, notes, and snippets.

@quiver
Created March 14, 2020 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quiver/21ad5b2f6f4e934a5e3687317fe09c3e to your computer and use it in GitHub Desktop.
Save quiver/21ad5b2f6f4e934a5e3687317fe09c3e to your computer and use it in GitHub Desktop.
Sample script to create CloudFront invalidation
'''
Invalidate Amazon CloudFront paths
API
- https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_CreateInvalidation.html
- https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudfront.html#CloudFront.Client.create_invalidation
'''
import random
import time
import boto3
client = boto3.client('cloudfront')
DistributionId = 'EXAMPLE'
Items = ['/hello.jpg']
def unique_string(prefix='cli'):
''' borrowed from aws-cli/awscli/customizations/cloudfront.py
'''
return '%s-%s-%s' % (prefix, int(time.time()), random.randint(1, 1000000))
res = client.create_invalidation(
DistributionId=DistributionId,
InvalidationBatch = {
'Paths' : {
'Quantity': len(Items),
'Items': Items,
},
'CallerReference' : unique_string()
}
)
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment