0% found this document useful (1 vote)
199 views

Boto3 Cheat Sheet

This document provides a summary of how to use the boto3 library to interact with Amazon Web Services. It outlines how to install boto3 and configure AWS credentials. It then demonstrates how to connect to EC2 and query existing instances, print details about instances, and filter them by properties like name or state. Finally, it shows how to create a new EC2 instance by specifying parameters like the AMI, security groups, subnet, and instance type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
199 views

Boto3 Cheat Sheet

This document provides a summary of how to use the boto3 library to interact with Amazon Web Services. It outlines how to install boto3 and configure AWS credentials. It then demonstrates how to connect to EC2 and query existing instances, print details about instances, and filter them by properties like name or state. Finally, it shows how to create a new EC2 instance by specifying parameters like the AMI, security groups, subnet, and instance type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

boto3 Cheat Sheet

by wtranmer via cheatography.com/27356/cs/7911/

Setup Querying EC2 (cont) Creating Instances

To use boto3, you must first install python. print( instance ) # Gather instance details
There are a number of distri​butions available; # Print list of instances by name tag # ID of the AMI (Amazon Machine
among the most popular are Anaconda3, for instance in instan​ces: Image) that the VM will use for OS
Active​Python, and PyPy.
print( [tag['​Value'] for tag in `# This is the ami for Window Server 2012 R2
I prefer Anaconda3 because of the large
instan​ce.tags if tag.ge​t('​Key') == 64-bit
number of pre-in​stalled packages. This does
ami_id = ami-3d​787d57
'Name'] )
however also mean it is one of the largest
# IDs of the Security Groups to be
distri​but​ions. 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' secur​ity​_gr​oup_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 config​ure to set
'shutt​ing​-down' }, decrypt the admini​strator password
your creden​tials and default region.
{ 'Code' : 48, 'state' : keyfi​le_name =
aws configure
'termi​nated' }, # ARN of the IAM instance profile
Now you are ready to roll.
{ 'Code' : 64, 'state' : to be assigned to the VM
iam_p​rof​ile_arn =
Services 'stopping' },
{ 'Code' : 80, 'state' : 'arn:a​ws:​iam​::1​234​567​890​12:​ins​tan​ce
boto3 includes access to almost all of the AWS
'stopped' }, -​pro​fie​l/m​ypr​ofile
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 insta​nce​_states = { insta​nce​_type = 't2.mi​cro'

service using boto3's api for that object. 'p​end​ing': [], # Disk drive capacity in GB
For instance, to create a new EC2 instance, 'r​unn​ing': [], disk_size = 120
you will create a resource object that is # UserData. Code block to execute
's​hut​tin​g​down' : [],
connected to 'ec2.' Then with that object, you
't​erm​inated' : [], on first instance launch
will call a function to create the instance and
's​top​ping' : [], user_data = ' '
pass the approp​riate parameters to the
# Create the instance
function. 's​topped' : [],
} ec2.c​rea​te_​ins​tan​ces(
Im​ageId = ami_id,
Querying EC2 for instance in instan​ces:
in​sta​nce​_na​me=​[ta​g['​Value'] for Mi​nCount = 1,
Connect to EC2
Ma​xCount = 1,
To create an EC2 resource object tag in instan​ce.tags if
Ke​yName = keyfil​e_n​ame,
import boto3 tag.ge​t('​Key') == 'Name']
Se​cur​ity​Gro​upIds =
ec2 = boto3.r​es​our​ce(​'ec2') in​sta​nce​_st​ate​s[i​nst​anc​e.s​tat​e['​N
am​e']​].a​ppend( instan​ce_​name[0] ) securi​ty_​gro​up_​ids,
instances = ec2.in​sta​nce​s.a​ll()
In​sta​nceType = instan​ce_​type,
# Print raw list of instan​ces for i in instan​ce_​sta​tes:
Bl​ock​Dev​ice​Map​pings = [
for instance in instan​ces: in​sta​nce​_st​ate​s[i​].s​ort()
{
pr​int(i + ' instan​ces​\n--')
for name in instan​ce_​sta​tes[i]: 'D​evi​ceN​ame': '/dev/​sda1',

print( name ) 'Ebs': {

pr​int​('')

By wtranmer Not published yet. Sponsored by CrosswordCheats.com


cheatography.com/wtranmer/ Last updated 18th April, 2016. Learn to solve cryptic crosswords!
Page 1 of 2. http://crosswordcheats.com
boto3 Cheat Sheet
by wtranmer via cheatography.com/27356/cs/7911/

Creating Instances (cont)

'V​olu​meS​ize': disk_s​ize,
'D​ele​teO​nTe​rmi​nat​ion': True,
'V​olu​meT​ype': 'gp2',
}
],
Ia​mIn​sta​nce​Profile = {
'Arn': arn_pr​ofile
},
Su​bnetId = subnet​_id,
Us​erData = user_data
)

By wtranmer Not published yet. Sponsored by CrosswordCheats.com


cheatography.com/wtranmer/ Last updated 18th April, 2016. Learn to solve cryptic crosswords!
Page 2 of 2. http://crosswordcheats.com

You might also like