Skip to content

Instantly share code, notes, and snippets.

@harperreed
Created January 14, 2011 21:46
Show Gist options
  • Save harperreed/780318 to your computer and use it in GitHub Desktop.
Save harperreed/780318 to your computer and use it in GitHub Desktop.
creates a custom origin for cloudfront. EASY CDN!
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
import datetime
from boto.cloudfront import CloudFrontConnection
import os
import sys
rootdir = '/path/to/static/assets/'
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment