Edwardr
Edwardr

Reputation: 2956

Boto script freezing when trying to deploy and configure EC2 instances

I'm using boto in Python to automate some of my EC2 workflow.

The issue is very strange - the script appears to freeze on an assignment of a simple variable, but it is continuing in the background. Eventually the script prints everything out.

When I repeat the script line by line in iPython there are no problems and no freezing or waiting (beyond what you expect when talking the AWS). The output just seems to freeze when I run it as a Python script, until the script is complete.

The script:

def deploy_web_db_ami(a_key, a_pri, db_vol_id, db_vol_zone, db_vol_mnt):
    '''deploys a fresh instance for using as the web / db server.  
    '''

    #Connect to the EC2
    print "Connecting to AWS"
    conn = EC2Connection(a_key, a_pri)

    # get a ref to the image we want to install
    print "Searching for desired AMI image"
    image = conn.get_all_images(image_ids='ami-fd589594')


    print "Spinning up instance. . ."
    # Launch the image using our desired settings.
    reservation = image[0].run(key_name='my_kp', 
                               security_groups=['ssh'],
                               instance_type='t1.micro)
    print("we get this far before output freezes")
    ins = reservation.instances[0]


    print "Waiting for instance to move from pending to running. ",
    while (ins.state.lower() == u'pending'):
        print ". ",
        ins.update()
        time.sleep(5)

    time.sleep(5) # in case ssh is not up yet
    print "Instance %s's state changed to: %s" (ins.id, ins.state)
    if ins.state.lower() == u'running':
        # instance is up and running
        # Attach the db EBS volume
        print "Attaching DB EBS volume."
        conn.attach_volume(db_vol_id, ins.id, db_vol_mnt)
        p_dns = ins.dns_name
    else:
        print "ERROR - INSTANCE NOT RUNNING.:: %s" % ins.state

    print "All done!"
    return (ins.id, p_dns)

def total_web_deploy():
    deploy_web_db_ami('xxx', 'xxx', 'the id', 'the zone', '/mnt/sdf')
    some_other_functions(). .. 

I'm running the script from the command line using fab total_web_deploy

The output will look like this:

Connecting to AWS
Search for desired AMI image
Spinning up instance. . .
we get this far before output freezes

Then we will have to wait for the instance and everything to be finished before the rest of the script prints out. It's clearly been working away in the background though.

Waiting for instance to move from pending to running.  .  .  .  .  .  .  .  .  .  .  Instance i-95c389f6's state changed to: running
Attaching DB EBS volume.
All done!

Any ideas?

EDIT I've clarified the question.

Upvotes: 2

Views: 671

Answers (1)

cloudartisan
cloudartisan

Reputation: 656

Perhaps your output is being buffered.

Try writing directly to stderr in order to rule that out:

import sys
sys.stderr.write("Equivalent message here\n")

Or:

import sys
sys.stdout.write("Equivalent message here\n")
sys.stdout.flush()

Upvotes: 2

Related Questions