Thanks to visit codestin.com
Credit goes to github.com

Skip to content

pdanford/launch_ec2_spots

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Script to launch spot instances in Amazon's EC2 as specified by a json launch specification file.

Default behavior is to only submit a launch request to AWS and return immediately.
See --wait and --fullwait for other levels of spot request fulfillment confirmation options.
Use -h to see other options.

Requires:

  1. awscli (sudo pip install awscli).
  2. A json launch specification file.
  3. python 2.7 or above.

Note that awscli requires the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to be set, or alternatively, the configuration file ~/.aws/config on Linux/OSX or C:\Users\USERNAME\.aws\config on Windows. A minimal example of this file is:

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

Example json launch specification file required by launch_ec2_spots.py:

{
  "INSTANCE_COUNT": "2",
  "MAX_SPOT_PRICE": "0.08",
  "USER_DATA_FILE_NAME":"ami_bootstrap",
  "ImageId": "ami-bba18dd2",
  "InstanceType": "t1.micro",
  "KeyName": "amazon-ssh-pub-key-tag",
  "Placement": { "AvailabilityZone": "us-east-1c", "GroupName": "" },
  "SecurityGroups": ["default"]
}

Any additional Amazon launch specification defined fields may be added as needed.
See here (the --launch-specification section) for more info.

USER_DATA_FILE_NAME value is optional and specifies a file containing user data to be made available to all launched AMIs. Note that if the user data begins with an interpreter (e.g. #!/bin/bash), an Amazon supplied AMI will regard the user data as a bootstrap script and automatically run it (as root) at AMI boot time.

EC2 launch region is derived from:

  1. The AWS_DEFAULT_REGION environment variable.
  2. Adding a line like 'region = us-east-1' to ~/.aws/config under the [default] section.
  3. Placement:AvailabilityZone specified in launch spec file as in the above example; if AvailabilityZone is set to something like "us-east-1" instead of a proper "us-east-1c", then the placement will be in the region us-east-1 and the actual zone will be chosen by Amazon. In both cases, the region is derived from the AvailabilityZone value and overrides any region set by method 1 or 2. Setting a proper AvailabilityZone may be desirable when best historical spot price zone is a concern.

Troubleshooting:
Note that some launches may result in requests never finishing (i.e. becoming zombies). As an example, if the user data referenced by USER_DATA_FILE_NAME is greater than 16k, the AWS request may stall and not return an error. Ctrl-C can be used while the script is in a waiting state to abort the launch and kill the stalled requests. Also, if you get an error back like "utf-8" or "ascii", it means the user data you're trying to send is not text (i.e. not text or base64 encoded data).

pdanford - Mar 2014. MIT License, etc.

About

I'd been using EC2 spot instances a lot to do stock analysis stuff, but it was a pain to specify and wait for the AMI instances to boot (aws cli is a bit low level). So I wrote this script to automate the process and pipe the output to stdout for use in other scripts.

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages