Using Fog For Amazon Cloudfront

There are a lot of ways to set up a CDN and you don’t even need to be a programmer to do so. Cyberduck seems to have worked for some people.

Found fog gem recently and it’s working except that their documentation hasn’t been updated. “Enabled” option is now required.

require 'rubygems'
require 'fog'

cdn = Fog::CDN.new({
  provider: 'AWS',
  aws_access_key_id: "xx", 
  aws_secret_access_key: "xx"
})

data = cdn.post_distribution({
  'CustomOrigin' => {
    'DNSName' => 'xx.yourdomain.com',
    'HTTPPort' => 80, 
    'OriginProtocolPolicy'  => 'match-viewer'
  }, 
  'CNAME' => 'cdn.yourdomain.com',
  'Enabled' => true
})

# parse the response for stuff you'll need later
distribution_id   = data.body['Id']
caller_reference  = data.body['CallerReference']
etag              = data.headers['ETag']
cdn_domain_name   = data.body['DomainName']

Hope that helps someone.