Shell scipts to provision an EC2 instance from an existing Vagrantfile (Vagrant) and Cheffile (Librarian) config files.
This project is inspired by vagrant-ec2 (and vagrant-ec2-r) but works with coobooks with dependencies by relying on Librarian.
It's an easy solution for provisionning and EC2 instance.
These scripts have been tested only on Mac OS X 10.7 but should works on many unix based system, and if not, the script is simple fix it and make a pull request ;)
- VirtualBox 4
- Vagrant (
gem install vagrant
) - Librarian (
gem install librarian
) - EC2 API Tools
If you need an developer-centric introduction to Vagrant and Librarian please refer to my posts on tumblr tumblr.nrako.com/tagged/vagrant.
On your local machine, you will need the following:
- Do steps 1-11 from this page.
- On Linux, instead of doing step 8, installing the EC2 command-line tools manually, they can be installed with
apt-get
. See the section on Linux below for more information. - Instead of step 12, put the following in your
~/.profile
. Make sure that theJAVA_HOME
is appropriate for your system; if you have a Mac, delete the line for Linux:
- On Linux, instead of doing step 8, installing the EC2 command-line tools manually, they can be installed with
# Setup Amazon EC2 Command-Line Tools
export EC2_HOME=~/.ec2
export PATH=$PATH:$EC2_HOME/bin
export EC2_PRIVATE_KEY=`echo $EC2_HOME/pk-*.pem`
export EC2_CERT=`echo $EC2_HOME/cert-*.pem`
export JAVA_HOME=`/usr/libexec/java_home` # On Mac
export JAVA_HOME=/usr/lib/jvm/default-java # On Ubuntu linux
- Make sure you have the JSON Ruby gem installed:
gem install --user-install json
- Create a key pair in the appropriate region. In this case the region is
us-west-1
, and we'll use the nameec2-us-west-1-keypair
:
ec2-add-keypair --region us-west-1 ec2-us-west-1-keypair > ~/.ec2/ec2-us-west-1-keypair
chmod 600 ~/.ec2/ec2-us-west-1-keypair
Note : ubuntu on us-east-1 has some temporary issue as today (29th Mai 2012)
After you do all these things, you will need to start a new terminal, or simply run all the export
lines (that you added to your ~/.profile
) from your command line.
source ~/.profile
Do the following each time you want to create a virtual machine on EC2.
Start up a new EC2 instance (ami-87712ac2
is a Ubuntu 12.04 64-bit server in region us-west-1
):
ec2-run-instances ami-87712ac2 --region us-west-1 --instance-type t1.micro --key ec2-us-west-1-keypair --user-data-file bootstrap.sh
Note that --user-data-file boostrap.sh
is important as it will install Chef-solo and Librarian and you may want to add -g <your security group>
.
Find its IP address with:
ec2-describe-instances --region us-west-1
After the machine boots up, provision it using the same recipes as the demo Vagrant machine machine:
./setup.sh <ip address> path_to_vagrantfile/ ~/.ec2/ec2-us-west-1-keypair
This will scp
secure copy Cheffile and dna.json to the EC2 instance, run librarian-chef install
to fetch the recipes and run chef-solo
.
It should print a lot of diagnostic info to the terminal. If it doesn't, wait a little while and try again.
You can ssh into the machine:
ssh -i ~/.ec2/ec2-us-west-1-keypair ubuntu@<ip address>
This will terminate your instances when you're finished:
ec2-terminate-instances --region us-west-1 <i-instance_id>
Just add six lines in the provisioning section of your Vagrantfile
so it looks like this:
config.vm.provision :chef_solo do |chef|
<your provisioning here>
# Generate the dna.json file used to describe the recipes to be executed by chef-solo on ec2
require 'json'
open('dna.json', 'w') do |f|
chef.json[:run_list] = chef.run_list
f.write chef.json.to_json
end
end
Inspired by vagrant-ec2-r from Winston Chang. Which is based on vagrant-ec2 from Keming labs. Librarian from Jay Feldblum.