Skip to content

Instantly share code, notes, and snippets.

@yagonobre
Last active April 12, 2024 13:25
Show Gist options
  • Save yagonobre/2666c13cd0f339eb00700226abc95195 to your computer and use it in GitHub Desktop.
Save yagonobre/2666c13cd0f339eb00700226abc95195 to your computer and use it in GitHub Desktop.
Invalidate Cloudfront
from __future__ import print_function
import boto3
import time
def lambda_handler(event, context):
path = "/" + event["Records"][0]["s3"]["object"]["key"]
bucket_name = event["Records"][0]["s3"]["bucket"]["name"]
client = boto3.client('s3')
tags = client.get_bucket_tagging(Bucket=bucket_name)
for tag in tags["TagSet"]:
if tag["Key"] == "distribution_id":
distribution_id = tag["Value"]
break
client = boto3.client('cloudfront')
invalidation = client.create_invalidation(DistributionId=distribution_id,
InvalidationBatch={
'Paths': {
'Quantity': 1,
'Items': [path]
},
'CallerReference': str(time.time())
})
Copy link

ghost commented May 16, 2019

thanks! came from your post: https://medium.com/@yagonobre/automatically-invalidate-cloudfront-cache-for-site-hosted-on-s3-3c7818099868

this works only for the files that have changed right? I mean, not the whole bucket in order to keep up with the free 1,000 invalidations per month limit.

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