Use boto run user-data in AWS instance
I am using boto to launch the instance run the user-code in the background, but find that it’s not so convenient to debug and get the error information. Here’s finally what I did. Use the traceback to get the error, use smtp to send back the error information to my email. There must be some other cleverer ways to do so, ha ~
import boto from boto.ec2.connection import EC2Connection import time ami_id = '----' # ami-ubuntu-64 (settedup) use t1.micro as instance type key_pair_name = '----' AWS_ACCESS_KEY_ID = '----' AWS_SECRET_ACCESS_KEY = '----' ec2conn = EC2Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) test_script_00 = """#!/bin/bash sudo apt-get update sudo apt-get install imagemagick """ test_script_01 = """#!/usr/bin/env python import boto from boto.s3.key import Key AWS_ACCESS_KEY_ID = '-------' AWS_SECRET_ACCESS_KEY = '----' bucket_name = 'demo-test' conn_s3 = boto.connect_s3(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY) bucket = conn_s3.get_bucket(bucket_name) k=Key(bucket) k.key='images/rose111105.jpg' k.copy('demo-test', 'images/rose111105copy6.jpg') """ test_script ="""#!/usr/bin/env python import smtplib import boto from boto.s3.key import Key import numpy as np import sys AWS_ACCESS_KEY_ID = '-----' # Your AWS_KEY AWS_SECRET_ACCESS_KEY = '---------' # Your Secret KEY def send_notice(msg='testing'): fromaddr = '******@gmail.com' toaddrs = '******@gmail.com' username = '******' password = '******' server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.login(username,password) server.sendmail(fromaddr, toaddrs, msg) server.quit() send_notice("start processing") #DO YOUR PROCESS send_notice("Finishing...") """ my_reservation = ec2conn.run_instances(ami_id, instance_type=instance_type, key_name=key_pair_name, user_data=test_script) instance = my_reservation.instances[0] while not instance.update() == 'running': time.sleep(5) instance.stop()
Categories: Amazon Web Services, Programming
Python, Source code
Comments (0)
Trackbacks (0)
Leave a comment
Trackback