Skip to content

Instantly share code, notes, and snippets.

@jolexa
Created October 24, 2018 14:07
Show Gist options
  • Save jolexa/e58ea2ec19cf3067d0ddfbdc98bbaf6d to your computer and use it in GitHub Desktop.
Save jolexa/e58ea2ec19cf3067d0ddfbdc98bbaf6d to your computer and use it in GitHub Desktop.
Invalidate CloudFront Cache with boto3
from time import time
import sys
import boto3
client = boto3.client('cloudfront')
# Uncomment this to pass a URL to the script
#def get_id(url):
# print("get_id args: {0}".format(url))
# # url: asdf.cloudfront.net
# # return: E2134123ASDF
# # where E2134123ASDF is the id of asdf.cloudfront.net
# paginator = client.get_paginator('list_distributions')
# response_iterator = paginator.paginate()
# for i in response_iterator:
# for j in i['DistributionList']['Items']:
# if j['Aliases']['Items'][0] == url:
# return j['Id']
response = client.create_invalidation(
#DistributionId=get_id(sys.argv[1]),
DistributionId=sys.argv[1],
InvalidationBatch={
'Paths': {
'Quantity': 1,
'Items': [
'/*'
],
},
'CallerReference': str(time()).replace(".", "")
}
)
@vonschnappi
Copy link

Thanks for that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment