Created
June 23, 2012 18:42
-
-
Save crowdmatt/2979403 to your computer and use it in GitHub Desktop.
Create a new Amazon CloudFront distribution for an S3 bucket in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Here is some sample code to create a CloudFront distribution programmatically from Ruby. | |
# Tested to be working on 06-23-2012. | |
# Amazon's documentation is wrong, so we're putting the XML here! | |
# | |
# We're using this at http://www.crowdmob.com/ to simplify our development process. | |
# | |
# Prerequisite: You'll need the simple_aws gem which you can get from `gem install simple_aws` | |
# | |
require 'simple_aws/s3' | |
require 'simple_aws/cloud_front' | |
S3_BUCKET_NAME = 'MYBUCKETNAME' | |
AWS_KEY = '...' | |
AWS_SECRET = '...' | |
def cloudfront_creation_xml(s3bucket) | |
"<DistributionConfig xmlns=\"http://cloudfront.amazonaws.com/doc/2012-05-05/\"><CallerReference>#{s3bucket}-creation-#{Time.now}</CallerReference><S3Origin><DNSName>#{s3bucket}.s3.amazonaws.com</DNSName></S3Origin><Aliases><Quantity>0</Quantity></Aliases><Comment/><Logging><Enabled>false</Enabled><Bucket>#{s3bucket}-logs-disabled.s3.amazonaws.com</Bucket><Prefix>#{s3bucket}.logprefix</Prefix></Logging><TrustedSigners><Enabled>false</Enabled><Quantity>0</Quantity></TrustedSigners><Enabled>true</Enabled></DistributionConfig>" | |
end | |
cloudfront = SimpleAWS::CloudFront.new AWS_KEY, AWS_SECRET | |
distribution = cloudfront.post "/distribution", | |
:body => cloudfront_creation_xml(S3_BUCKET_NAME) | |
puts "- Cloudfront distribution created `#{distribution.domain_name}`" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The other libraries were terrible - your gist is the only game in town :-) I fixed it by removing:
<TrustedSigners><Enabled>false</Enabled> ... </TrustedSigners>
To confirm: for a public distribution, do not add the TrustedSigners section, or you will get missing key errors...