Skip to content

Instantly share code, notes, and snippets.

@sampsyo
Created July 23, 2010 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sampsyo/486931 to your computer and use it in GitHub Desktop.
Save sampsyo/486931 to your computer and use it in GitHub Desktop.
quick EC2 sandbox creator
#!/usr/bin/env python
"""Quickly get a temporary Ubuntu EC2 instance, SSH into it, and then
terminate the instance on logout.
"""
import boto
import subprocess
import time
# Set your AWS credentials.
EC2_ACCESS = ''
EC2_SECRET = ''
AWS_KEYPAIR = ''
KP_PATH = ''
# These should be fine.
AMI_ID = 'ami-2d4aa444' # 10.04 32-bit instance-store us-east-1
SSH_BIN = 'ssh'
SSH_USER = 'ubuntu'
if __name__ == '__main__':
conn = boto.connect_ec2(EC2_ACCESS, EC2_SECRET)
image = conn.get_image(AMI_ID)
inst = image.run(key_name=AWS_KEYPAIR).instances[0]
try:
print 'Starting up...'
while inst.state == 'pending':
time.sleep(3)
inst.update()
print 'Instance started. Waiting for boot...'
time.sleep(15)
if inst.state != 'running':
print 'Startup failed! State: ' + instance.state
sys.exit(1)
print 'Connecting with SSH...'
arg = '%s@%s' % (SSH_USER, inst.dns_name)
print arg
subprocess.call((SSH_BIN, '-i', KP_PATH, arg))
finally:
print 'Destroying instance...'
inst.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment