Skip to content

Instantly share code, notes, and snippets.

@a-hisame
Created July 30, 2014 12:39
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 a-hisame/1d8f05a8d0fe9016c1e5 to your computer and use it in GitHub Desktop.
Save a-hisame/1d8f05a8d0fe9016c1e5 to your computer and use it in GitHub Desktop.
Create AWS Spot Instance Automation
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
スポットインスタンスを自動的に指定個数リクエストをかけてインスタンスを作る。
作成したインスタンスIDを標準出力する。
Python2.7 + boto で動作確認。
キーを残したくない場合はコネクションの作成を工夫すること。
'''
import sys
import time
import logging
import argparse
import boto.ec2
def _wait_for_all_launch(conn, responses, max_trial_count=15):
logging.info('Wait for ALL Request until status is open...')
time.sleep(10)
dict = {}
for cnt in range(0, max_trial_count):
if len(dict) == len(responses):
logging.info('All Request Accepted.')
break
for response in responses:
req_id = u'{0}'.format(response.id)
if dict.has_key(req_id):
continue
try:
logging.info('request id "{0}" is waiting now...'.format(req_id))
res = conn.get_all_spot_instance_requests(request_ids=[req_id])[0]
if res.instance_id:
dict[req_id] = res.instance_id
except Exception as e:
logging.error('Exception has caused!')
logging.exception(e)
time.sleep(30)
return dict
def args_parser():
parser = argparse.ArgumentParser(description=u'ccms task pollerd')
parser.add_argument('--price', type=str, help=u'spot instance request price (example, 0.01)', required=True)
parser.add_argument('--ami_id', type=str, help=u'AMI ID', required=True)
parser.add_argument('--count', type=int, default=1, help=u'number of instances', required=False)
parser.add_argument('--region', type=str, help=u'AWS Region', required=True)
parser.add_argument('--access_key', type=str, help=u'AWS Access Key', required=True)
parser.add_argument('--secret_access_key', type=str, help=u'AWS Secret Access Key', required=True)
return parser
def main(conn, price, image_id, count):
# responses is a list of SpotInstanceRequest
# add parameter such as keypair, subnet-id, security-group if needed.
responses = conn.request_spot_instances(
price,
image_id,
count = count)
res = _wait_for_all_launch(conn, responses)
logging.info('Accept spot request instances')
for k,v in res.items():
logging.info('request-id: {0}, instance-id: {1}'.format(k, v))
print v
if __name__ == '__main__':
args = args_parser().parse_args(sys.argv[1:])
conn = boto.ec2.connect_to_region(args.region,
aws_access_key_id = args.access_key,
aws_secret_access_key = args.secret_access_key)
main(conn, args.price, args.ami_id, args.count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment