I have used the boto library to instantiate new EC2 instances for a few of my project and then used the fabric to perform the configuration of the new EC2 systems once they have booted.
To use them you first need to create your access tokens for Amazon itself. These tokens, for boto are placed into your source file and used in the connect
methods for the EC2 instances or you can place them into a .boto
file in your home directory. The latter is much easier.
What you need from Amazon is the following:
- Security group name and sshkey.
- Ami id for the instance you wish to create.
- The type of instance you want, e.g: m1.small
With the above information you will make a call to the run_instance
method with the above information:
instance = conn.run_instances( ami.ami_id, key_name=ami.sshkey.name,
instance_type=server.game.instance_type,
security_groups=[server.game.security_group] )
instance = instance.instances[0]
while instance.update() == "pending":
time.sleep( 5 )
After this is done a new instance should start to boot up in your Amazon control panel. You need to check the instance's status and once it is in a running
state you can then use Fabric to configure the instance.
with settings( host_string="ec2-user@%s" % instance.ip_address,
key_filename=os.path.join( os.getenv( "HOME" ),
".ssh", "%s.pem" % ami.sshkey.name ),
connection_attempts=5, timeout=60 ):
...
sudo( "yum -y install mysql mysql-devel" )
...
With the above it will run the fabric commands in the same file, but a more controlled method of using Fabric is via fab files. These are explained better on the Fabric documentation.
The above is what I use to automate the creation and setup of instances as needed, so tweak the code to suit your fit.