Middleman and AWS CLI S3 Sync
Dec 2, 2013
The aws cli allows a command line interface with your Amazon services.
I've recently integrated the
aws cli with my middleman site for syncing changes to my S3 bucket. Here is how I did it.
Install aws cli
See the getting started guide.
Store your credentials in a profile file
Create the AWS config file for storing your keys
$ mkdir ~/.aws/ $ touch ~/.aws/config
Add the default profile. The default profile is used when you use
aws without specifying a profile.
[default] aws_access_key_id=AWS_ACCESS_KEY_ID aws_secret_access_key=AWS_SECRET_ACCESS_KEY region=us-east-1
Add a custom profile, if you like, to the config file
Honestly, I'm not sure why I use a custom profile.
I think it seemed like a good idea when I first set up
[profile luk3] aws_access_key_id=AWS_ACCESS_KEY_ID aws_secret_access_key=AWS_SECRET_ACCESS_KEY region=us-east-1
Sync everything to S3
For this blog I store the aws command in a Makefile.
deploy: @bundle exec middleman build @aws --profile=luk3 s3 sync build/ s3://luk3thomas.com/ --acl=public-read --delete --cache-control="max-age=1576800000" --exclude "*.html" @aws --profile=luk3 s3 sync build/ s3://luk3thomas.com/ --acl=public-read --delete --cache-control="max-age=0, no-cache" --exclude "*" --include "*.html"
aws --profile=luk3uses the profile
synccommand only syncs the changes since the last upload.
build/is the target directory where the static HTML files live.
s3://luk3thomas.com/is my bucket.
--acl=public-readmakes the files viewable by everyone. See the manual for other options.
--deletedeleles any files from S3 that are not found in the
--cache-control="max-age=0, no-cache"sets the cache expiration. I don't want the browser caching my static pages.
--exclude "*"exludes all files. The exclude option is used in conjunction with the
--include "*.html"includes all the HTML files.
For more information on the
sync command, read the manual.