Sunday, October 12, 2008

Syncing contents between EC2 & S3 - s3sync

In my ongoing work on autoscaling Web services on EC2, when an Axis2 EC2 instance boots up, it needs to load all the configuration files & the service+module repository from Amazon S3. While searching for an appropriate tool to do this, I came across s3sync



Installation
1. Install ruby
On Ubuntu, it was apt-get install ruby ruby1.9
2. Install openssl
apt-get install openssl
3. Install openssl-ruby
apt-get install libopenssl-ruby

Before Running
Make sure that the following environment variables are exported.
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
In order to obtain the values for those, you need to register with Amazon Web Services (AWS).

Examples from the s3sync README
1. Put the local etc directory itself into S3
s3sync.rb -r /etc mybucket:pre
(This will yield S3 keys named pre/etc/...)

2. Put the contents of the local /etc dir into S3, rename dir:
s3sync.rb -r /etc/ mybucket:pre/etcbackup
(This will yield S3 keys named pre/etcbackup/...)

3. Put contents of S3 "directory" etc into local dir
s3sync.rb -r mybucket:pre/etc/ /root/etcrestore
(This will yield local files at /root/etcrestore/...)

4. Put the contents of S3 "directory" etc into a local dir named etc
s3sync.rb -r mybucket:pre/etc /root
(You need to first create the directory, /root/etc. This will yield local files at /root/etc/...)

5. Put S3 nodes under the key pre/etc/ to the local dir etcrestore
**and create local dirs even if S3 side lacks dir nodes**
s3sync.rb -r --make-dirs mybucket:pre/etc/ /root/etcrestore
(This will yield local files at /root/etcrestore/...)


Only the contents that have been changed in the S3 bucket will be loaded from S3. Now that I can sync between my EC2 instance & S3 bucket, I simply need to write an init script that loads the configuration & repository from a specified S3 bucket. This init script needs to be automatically run when the instance boots up. This can be done easily using chkconfig. chckconfig can be downloaded from here. Installation instructions are available here. Create the script, say syncs3, place it in /etc/init.d, and add it as follows:

chkconfig --add syncs3

Remember to rebundle your instance into a new image. Now whenever an instance of the new image starts up, it will try to load the configuration from the S3 bucket.




0 comments: