Last active
April 12, 2024 13:25
-
-
Save yagonobre/2666c13cd0f339eb00700226abc95195 to your computer and use it in GitHub Desktop.
Invalidate Cloudfront
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
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()) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.