Skip to content

Instantly share code, notes, and snippets.

@rosskarchner
Created March 8, 2019 15:40
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 rosskarchner/8d3a5212cd4d602820022ccf888a3bda to your computer and use it in GitHub Desktop.
Save rosskarchner/8d3a5212cd4d602820022ccf888a3bda to your computer and use it in GitHub Desktop.
automatically invalidate URL's in cloudfront when a file is updated in S3
import boto3
from botocore.vendored import requests
client = boto3.client('cloudfront')
def handler(event, context):
for record in event['Records']:
key = record['s3']['object']['key']
items = [key]
if key.endswith('index.html'):
items.append(key[:-10])
response = client.create_invalidation(
DistributionId='E2KMYV1XU86AEJ',
InvalidationBatch={
'Paths': {
'Quantity': len(items),
'Items': ['/'+item for item in items]
},
'CallerReference': record['eventTime'] + '/' + key
}
)
@rosskarchner
Copy link
Author

(attached to the ObjectCreated event on an S3 bucket)

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