Created
March 8, 2019 15:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(attached to the ObjectCreated event on an S3 bucket)