Created
December 23, 2014 15:54
-
-
Save davebiffuk/61eeab9b0a62053d171e to your computer and use it in GitHub Desktop.
start an AWS image, display the ssh command line to access it, and clean up afterwards
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python -u | |
import boto.ec2 | |
import time | |
import subprocess | |
# debian-wheezy-amd64-pvm-2014-09-26-ebs - ami-3051f047 | |
# ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-20140927 - ami-f0b11187 | |
instance = "ami-3051f047" | |
conn = boto.ec2.connect_to_region("eu-west-1") | |
res = conn.run_instances( instance, key_name='dh3awskey', | |
instance_type='t1.micro', | |
security_groups=['ssh only']) | |
inst = res.instances[0] | |
print "waiting for instance", inst.id, "to start", | |
while True: | |
time.sleep(2) | |
s = conn.get_all_instance_status(instance_ids=[inst.id]) | |
if len(s) == 0: | |
print ".", | |
continue | |
s = s[0].instance_status.status | |
print ":", | |
if s == "ok": | |
break | |
hostname = conn.get_only_instances(instance_ids=[inst.id])[0].public_dns_name | |
print "instance hostname is", hostname | |
print "ssh -o StrictHostKeyChecking=no -i ~/.ec2/dh3awskey.pem admin@" + hostname | |
raw_input("press Enter to terminate instance...") | |
conn.terminate_instances(instance_ids=inst.id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment