mikemansell Posted December 23, 2010 Report Share Posted December 23, 2010 If you are looking at using Amazon S3/CloudFront to host a static website, I have come up with a nice little script (requires s3cmd from http://s3tools.org/s3cmd) that allows you to create a bucket and distribution for your site in a relatively seamless fashion.What it does:Creates an S3 bucket (or uses an existing one)Uploads ALL of the files within a specified folder tree into the bucketMakes all of the items "public"Optionally creates a CloudFront distribution and sets a CNAME and default root object (e.g. index.html) based on your input.Below is the script. Obviously you're going to need s3cmd and an existing Amazon Web Services account with S3 and CloudFront enabled. The result will ultimately be a fast CDN at Amazon's very reasonable price.Be sure to save this as a bash script and set it to executable (chmod +x).clearecho "S3/CloudFront Website Uplaoder"echoecho "This utility will take advantage of AWS to host a website."echo "The end result will be a cost-effective CDN-based website."echoecho "Please note:"echo " - You must have an existing AWS account w/ S3 + CF"echo " - You must have s3cmd 1.0.0 + installed on this computer"echo " - Your API info can be found in your AWS account settings"echo " - All files will be public and filetypes will be guessed"echo " - An S3 bucket will be created if it doesn't exist"echo " - A CloudFront distribution can be created if specified"echoecho "--------------------------------------------------------------"echoecho "Please enter your API key:"read API_KEYechoecho "Please enter your API secret:"read API_SECechoecho "Please enter the local folder that contains your site:"read LOC_DIRechoecho "Please enter the S3 bucket where the site will be uploaded to:"read REM_BUCechoecho "Set up a CloudFront distribution - 'true' or 'false'"echo "Use 'true' if you want to take advantage of edge locations or default root objects."echo "DO NOT use 'true' if you have an existing distribution on the given bucket."read CFT_SETif [ "$CFT_SET" = "true" ]; then echo echo "Please enter the default root object (index.html) for ClodFront:" read CFT_DRE echo echo "Please enter the CNAME that you will use for the CF distribution:" read CFT_CNMfiechoecho "--------------------------------------------------------------"echoecho "Checking to make sure that all of the needed info was input..."echoif [ "$API_KEY" != "" ] && [ "$API_SEC" != "" ] && [ "$LOC_DIR" != "" ] && [ "$REM_BUC" != "" ]; then echo "All of your information appears to have been entered." echo "Mind you, the validity of this information has yet to be validated." echo echo "--------------------------------------------------------------" echo else echo "YOU ARE MISSING FIELD DATA. TRY AGAIN." echo echo "--------------------------------------------------------------" echo exitfiecho "Making sure that your local directory is valid..."echoif [ -e $LOC_DIR ]; then echo "Local directory has been validated!" echo echo "--------------------------------------------------------------" echo else echo "THE LOCAL DIRECTORY YOU SPECIFIED DOES NOT EXIST. TRY AGAIN." echo echo "--------------------------------------------------------------" echo exitfiecho "Making sure that you have s3cmd installed..."echoif [ "`whereis s3cmd`" != "s3cmd:" ]; then echo "s3cmd appears to be installed. All is good." echo echo "--------------------------------------------------------------" echo else echo "S3CMD IS NOT INSTALLED. TRY AGAIN." echo echo "--------------------------------------------------------------" echo exitfiecho "Creating validation file..."echoS3_FILE="`dirname $0`/`date +%Y%m%d%H%M%S`s3cmd.tmp"echo "$S3_FILE"echo "access_key = $API_KEY" >> $S3_FILEecho "secret_key = $API_SEC" >> $S3_FILEtail $S3_FILEechoecho "--------------------------------------------------------------"echoecho "STARTING THE UPLOAD PROCESS!"echos3cmd --config=$S3_FILE mb s3://$REM_BUCs3cmd --config=$S3_FILE sync --acl-public --guess-mime-type $LOC_DIR/ s3://$REM_BUCif [ "$CFT_SET" = "true" ]; then s3cmd --config=$S3_FILE cfcreate s3://$REM_BUC s3cmd --config=$S3_FILE cfmodify --cf-default-root-object=$CFT_DRE s3://$REM_BUC CFT_DMN="`s3cmd --config=$S3_FILE cfmodify --cf-add-cname=$CFT_CNM s3://$REM_BUC | grep DomainName`"fiechoecho "--------------------------------------------------------------"echoecho "The details for your new configuration is as follows:"echo "S3Bucket: $REM_BUC"if [ "$CFT_SET" = "true" ]; then echo "$CFT_DMN" echo "DfltRtObjt: $CFT_DRE" echo "CNAME: $CFT_CNM" echo echo "You will want to create a DNS record pointing your CNAME to your CloudFront distribution ('DomainName')." echo echo "CloudFront takes a bit of time to set up." echo "Patience is a virtue."fiechoecho "--------------------------------------------------------------"rm $S3_FILE Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.