-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvalidateCFcache.py
More file actions
34 lines (30 loc) · 1.22 KB
/
InvalidateCFcache.py
File metadata and controls
34 lines (30 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# When you upload an object to your s3 bucket, this Lambda function will be invoked. The function pretty much extracts the
# keyname and makes a invalidate cache API call against CloudFront. You also need a caller reference when making API call
# against CloudFront and for that we're using timestamp.
import json
import boto3
client = boto3.client('cloudfront') #CFClient for boto
import time
import urllib
from pprint import pprint
def lambda_handler(event, context):
print("Received event: " + json.dumps(event)) #Lambda event handler for boto3 + Print events on CW logs
objectname = event['Records'][0]['s3']['object']['key'] #extracts the keyname/path name from the s3 event log
ObjectListToInvalidate= []
ObjectListToInvalidate.append(objectname)
timestring = str(time.time())
timestringSecond = (timestring.split("."))
print (timestringSecond [0])
mystr1 = "/"+ ObjectListToInvalidate[0]
response = client.create_invalidation(
DistributionId='E1OAHO3XQ1HAUL', #update this with your distribution ID
InvalidationBatch={
'Paths': {
'Quantity': 1,
'Items': [
mystr1,
]
},
'CallerReference': timestringSecond [0]
}
)