-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf_invalidate.py
executable file
·44 lines (38 loc) · 1.13 KB
/
cf_invalidate.py
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
35
36
37
38
39
40
41
42
43
44
"""cf_invalidate.py"""
import sys
import time
import boto3
# Get domain name from command line argument
DOMAIN = sys.argv[1]
# Create CloudFront client
client = boto3.client("cloudfront")
# Get all CloudFront distributions
CF_DISTS = client.list_distributions()
# Find the distribution for the given domain name
DIST_ID = None
for distribution in CF_DISTS["DistributionList"]["Items"]:
if (
"Items" in distribution["Aliases"]
and distribution["Aliases"]["Items"][0] == DOMAIN
):
DIST_ID = distribution["Id"]
break
# Invalidate the path /* for the distribution
if DIST_ID:
ts = time.time()
resp = client.create_invalidation(
DistributionId=DIST_ID,
InvalidationBatch={
"Paths": {
"Quantity": 1,
"Items": [
"/*",
],
},
# Unique string for each request for invalidation
"CallerReference": f"invalidate_all {ts}",
},
)
print(f"Invalidated path /* for distribution - {DOMAIN} - {DIST_ID}\n{resp}")
else:
print(f"No distribution found for domain {DOMAIN}")