Professional Documents
Culture Documents
To use boto3, you must first install python. print( instance ) # Gather instance details
There are a number of distributions available; # Print list of instances by name tag # ID of the AMI (Amazon Machine
among the most popular are Anaconda3, for instance in instances: Image) that the VM will use for OS
ActivePython, and PyPy.
print( [tag['Value'] for tag in `# This is the ami for Window Server 2012 R2
I prefer Anaconda3 because of the large
instance.tags if tag.get('Key') == 64-bit
number of pre-installed packages. This does
ami_id = ami-3d787d57
'Name'] )
however also mean it is one of the largest
# IDs of the Security Groups to be
distributions. But as the saying goes, "It is # Print list of instances by state
assign to the VM
better to have and not need than to need and states = (
not have." { 'Code' : 0, 'state' : 'pending' security_group_ids = []
Once you have installed your Python of choice, }, # Single subnet id in which VM will
use the pip installer to install boto3. be started
{ 'Code' : 16, 'state' :
pip install boto3 subnet_id =
'running' },
Once boto3 is installed, install the Amazon # Key file that will be used to
{ 'Code' : 32, 'state' :
AWS CLI tools and run aws configure to set
'shutting-down' }, decrypt the administrator password
your credentials and default region.
{ 'Code' : 48, 'state' : keyfile_name =
aws configure
'terminated' }, # ARN of the IAM instance profile
Now you are ready to roll.
{ 'Code' : 64, 'state' : to be assigned to the VM
iam_profile_arn =
Services 'stopping' },
{ 'Code' : 80, 'state' : 'arn:aws:iam::123456789012:instance
boto3 includes access to almost all of the AWS
'stopped' }, -profiel/myprofile
services. To interact with these services, you
) # VM instance type / instance size
create a resource or client object that connects
to a particular service. Then you can use the instance_states = { instance_type = 't2.micro'
service using boto3's api for that object. 'pending': [], # Disk drive capacity in GB
For instance, to create a new EC2 instance, 'running': [], disk_size = 120
you will create a resource object that is # UserData. Code block to execute
'shuttingdown' : [],
connected to 'ec2.' Then with that object, you
'terminated' : [], on first instance launch
will call a function to create the instance and
'stopping' : [], user_data = ' '
pass the appropriate parameters to the
# Create the instance
function. 'stopped' : [],
} ec2.create_instances(
ImageId = ami_id,
Querying EC2 for instance in instances:
instance_name=[tag['Value'] for MinCount = 1,
Connect to EC2
MaxCount = 1,
To create an EC2 resource object tag in instance.tags if
KeyName = keyfile_name,
import boto3 tag.get('Key') == 'Name']
SecurityGroupIds =
ec2 = boto3.resource('ec2') instance_states[instance.state['N
ame']].append( instance_name[0] ) security_group_ids,
instances = ec2.instances.all()
InstanceType = instance_type,
# Print raw list of instances for i in instance_states:
BlockDeviceMappings = [
for instance in instances: instance_states[i].sort()
{
print(i + ' instances\n--')
for name in instance_states[i]: 'DeviceName': '/dev/sda1',
print('')
'VolumeSize': disk_size,
'DeleteOnTermination': True,
'VolumeType': 'gp2',
}
],
IamInstanceProfile = {
'Arn': arn_profile
},
SubnetId = subnet_id,
UserData = user_data
)