A Python script that requests a spot EC2 instance. Use InstanceId to control the instance.
Boto 3 Install
$ pip install boto3
-
Create Access Key for Your AWS Account. Instructions can be found at https://docs.aws.amazon.com/general/latest/gr/managing-aws-access-keys.html
-
Configure AWS CLI using AWS Access Key ID and AWS Secret Access Key
-
$ aws configure
-
AWS Access Key ID [None]: 'ACCESS_KEY_ID' AWS Secret Access Key [None]: 'SECRET_ACCESS_KEY' Default region name [None]:'Region' Default output format [None]: ENTER
-
-
To use multiple profiles set the system environment
AWSACC
with the name of the profile in.aws/config
List of a configure fields
tag
: Name of the instanceteam
,created_by
,application
: extra tagsami
: AMI_ID - Get the appropriate AMI ID for your region from http://aws.amazon.com/amazon-linux-ami/key_pair
: Named keypair on EC2 for instance creationsecurity_group
: Named security_group on EC2 for instance creationmax_bid
: Max bid price for spot instance. Current bid prices can be found at https://aws.amazon.com/ec2/spot/pricing/type
: Type of EC2 instance. List can be found at https://aws.amazon.com/ec2/instance-types/availability_zone
: AvailablityZone code (eg. 'us-west-1a'). List of regions can be found at https://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_regionsubnet_id
" subnet id for the AZproduct_description
: 'Linux/UNIX' or 'Windows'public_ip
Public IP you want to attach (can leave blank)iam_role
ARN of IAM role for instance (can leave blank)
*Refer to example-config.cfg
file.
You can add user data by creating a userdata.txt
file.
from ec2 import AutoEC2
ec2 = AutoEC2()
# show instances
ec2.instances
# create a new instance based on config file
inst = ec2.create("config.cfg")
# destroy the instance
ec2.destroy(inst["InstanceId"])