Automatically Sync EC2 to S3
AWS Nomads #3: A quick win — a script to keep your Cloud9 / EC2 working directory in sync with your public S3 bucket.
First of all, do not use this while working directly on a website that has traffic. You are going to save files, possibly accidentally, and they are going to be dropped into your public bucket instantly, and you are going to be making mistakes and breaking things.
If you don’t already have some form of staging system or development / production environment going for your live website, please consider that. (Note to self: future article.)
But while you are in the initial phase of rapid development or if you are syncing to a private staging environment that is not visible to the general public, this is a very pleasant and robust utility — and one that I have found many inferior or incomplete solutions for on StackOverflow and other blogs.
The gist of it (haha, yes the whole script is available in a gist, but also included below) is this:
- We give the local directory the same name as our s3 bucket.
- We run this script from the parent of our local directory with that bucket name.
- The script uses inotifywait to watch for changes on the developer box. EC2…