public
Last active

creates a custom origin for cloudfront. EASY CDN!

  • Download Gist
create_custom_origin.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import boto
# requires cf_customorigin branch
# https://github.com/boto/boto/tree/cf_customorigin
 
origin_domain = 'www.example.org'
cdn_cname = ['media.example.org']
caller_reference = 'exmaple Distribution'
 
c = boto.connect_cloudfront()
d = c.create_custom_distribution(
dns_name=origin_domain,
enabled=True,
caller_reference=caller_reference,
cnames=cdn_cname
)
print 'cname '+ cdn_cname + ' to ' d.domain_name
invalidate_cf_cdn.py
Python
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
import datetime
from boto.cloudfront import CloudFrontConnection
import os
import sys
 
rootdir = '../app/static/'
 
media_list = []
for root, subFolders, files in os.walk(rootdir):
root = root.replace('../app','')
for f in files:
media_list.append(root + '/'+ f)
media_list.append(root.replace('static/','') + f)
 
#any media that didn't make it (look in app.yaml)
default_media_list = [
'/favicon.ico',
'/robots.txt',
'/',
]
 
clear_list = media_list + default_media_list
 
aws_cloudfront_distributions = ['EXXXXXXXXXXXXXXX1','EXXXXXXXXXXXXXXX2']
 
print "About to clear " + str(len(clear_list)) + " items from cloudfront"
conn = CloudFrontConnection()
for cf_distro in aws_cloudfront_distributions:
try:
result = conn.create_invalidation_request(cf_distro, clear_list)
print "Cleared " +str(len(result)) + " items successfully from " + cf_distro
except:
print "error occurred. please try again in 15 minutes"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.