\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{wtranmer} \pdfinfo{ /Title (boto3.pdf) /Creator (Cheatography) /Author (wtranmer) /Subject (boto3 Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{D1D1B3} \definecolor{LightBackground}{HTML}{F9F9F5} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{boto3 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{wtranmer} via \textcolor{DarkBackground}{\uline{cheatography.com/27356/cs/7911/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}wtranmer \\ \uline{cheatography.com/wtranmer} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 11th May, 2016.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Setup}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{To use boto3, you must first install python. There are a number of distributions available; among the most popular are Anaconda3, ActivePython, and PyPy. \newline % Row Count 4 (+ 4) I prefer Anaconda3 because of the large number of pre-installed packages. This does however also mean it is one of the largest distributions. But as the saying goes, "It is better to have and not need than to need and not have." \newline % Row Count 9 (+ 5) Once you have installed your Python of choice, use the {\bf{pip}} installer to install boto3. \newline % Row Count 11 (+ 2) `pip install boto3` \newline % Row Count 12 (+ 1) Once boto3 is installed, install the Amazon AWS CLI tools and run {\bf{aws configure}} to set your credentials and default region. \newline % Row Count 15 (+ 3) `aws configure` \newline % Row Count 16 (+ 1) Now you are ready to roll.% Row Count 17 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Services}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{boto3 includes access to almost all of the AWS services. To interact with these services, you create a resource or client object that connects to a particular service. Then you can use the service using boto3's api for that object. \newline % Row Count 5 (+ 5) For instance, to create a new EC2 instance, you will create a resource object that is connected to 'ec2.' Then with that object, you will call a function to create the instance and pass the appropriate parameters to the function.% Row Count 10 (+ 5) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Querying EC2}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Connect to EC2}} \newline % Row Count 1 (+ 1) To create an EC2 resource object \newline % Row Count 2 (+ 1) `import boto3` \newline % Row Count 3 (+ 1) `ec2 = boto3.resource('ec2')` \newline % Row Count 4 (+ 1) `instances = ec2.instances.all()` \newline % Row Count 5 (+ 1) {\bf{\# Print raw list of instances}} \newline % Row Count 6 (+ 1) `for instance in instances:` \newline % Row Count 7 (+ 1) `~~print( instance )` \newline % Row Count 8 (+ 1) {\bf{\# Print list of instances by name tag}} \newline % Row Count 9 (+ 1) `for instance in instances:` \newline % Row Count 10 (+ 1) `~~print( {[}tag{[}'Value'{]} for tag in instance.tags if tag.get('Key') == 'Name'{]} )` \newline % Row Count 12 (+ 2) {\bf{\# Print list of instances by state}} \newline % Row Count 13 (+ 1) `states = (` \newline % Row Count 14 (+ 1) `~~\{ 'Code' : 0, 'state' : 'pending' \},` \newline % Row Count 16 (+ 2) `~~\{ 'Code' : 16, 'state' : 'running' \},` \newline % Row Count 18 (+ 2) `~~\{ 'Code' : 32, 'state' : 'shutting-down' \},` \newline % Row Count 20 (+ 2) `~~\{ 'Code' : 48, 'state' : 'terminated' \},` \newline % Row Count 22 (+ 2) `~~\{ 'Code' : 64, 'state' : 'stopping' \},` \newline % Row Count 24 (+ 2) `~~\{ 'Code' : 80, 'state' : 'stopped' \},` \newline % Row Count 26 (+ 2) `)` \newline % Row Count 27 (+ 1) `instance\_states = \{` \newline % Row Count 28 (+ 1) `~~'pending': {[}{]},` \newline % Row Count 29 (+ 1) `~~'running': {[}{]},` \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Querying EC2 (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`~~'shuttingdown' : {[}{]},` \newline % Row Count 1 (+ 1) `~~'terminated' : {[}{]},` \newline % Row Count 2 (+ 1) `~~'stopping' : {[}{]},` \newline % Row Count 3 (+ 1) `~~'stopped' : {[}{]},` \newline % Row Count 4 (+ 1) `\}` \newline % Row Count 5 (+ 1) `for instance in instances:` \newline % Row Count 6 (+ 1) `~~instance\_name={[}tag{[}'Value'{]} for tag in instance.tags if tag.get('Key') == 'Name'{]}` \newline % Row Count 8 (+ 2) `~~instance\_states{[}instance.state{[}'Name'{]}{]}.append( instance\_name{[}0{]} )` \newline % Row Count 10 (+ 2) `for i in instance\_states:` \newline % Row Count 11 (+ 1) `~~instance\_states{[}i{]}.sort()` \newline % Row Count 12 (+ 1) `~~print(i + ' instances\textbackslash{}n-{}-')` \newline % Row Count 13 (+ 1) `~~for name in instance\_states{[}i{]}: print( name )` \newline % Row Count 15 (+ 2) `~~print('')`% Row Count 16 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Creating Instances}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{\# Gather instance details}} \newline % Row Count 1 (+ 1) `\# ID of the AMI (Amazon Machine Image) that the VM will use for OS` \newline % Row Count 3 (+ 2) `\# This is the ami for Window Server 2012 R2 64-bit \newline % Row Count 5 (+ 2) `ami\_id = ami-3d787d57` \newline % Row Count 6 (+ 1) `\# IDs of the Security Groups to be assign to the VM` \newline % Row Count 8 (+ 2) `security\_group\_ids = {[}{]}` \newline % Row Count 9 (+ 1) `\# Single subnet id in which VM will be started` \newline % Row Count 10 (+ 1) `subnet\_id = ` \newline % Row Count 11 (+ 1) `\# Key file that will be used to decrypt the administrator password` \newline % Row Count 13 (+ 2) `keyfile\_name = ` \newline % Row Count 14 (+ 1) `\# ARN of the IAM instance profile to be assigned to the VM` \newline % Row Count 16 (+ 2) `iam\_profile\_arn = \seqsplit{'arn:aws:iam::123456789012:instance-profiel/myprofile`} \newline % Row Count 18 (+ 2) `\# VM instance type / instance size` \newline % Row Count 19 (+ 1) `instance\_type = 't2.micro'` \newline % Row Count 20 (+ 1) `\# Disk drive capacity in GB` \newline % Row Count 21 (+ 1) `disk\_size = 120` \newline % Row Count 22 (+ 1) `\# UserData. Code block to execute on first instance launch` \newline % Row Count 24 (+ 2) `user\_data = ' '` \newline % Row Count 25 (+ 1) {\bf{\# Create the instance}} \newline % Row Count 26 (+ 1) `ec2.create\_instances(` \newline % Row Count 27 (+ 1) `~~ImageId = ami\_id,` \newline % Row Count 28 (+ 1) `~~MinCount = 1,` \newline % Row Count 29 (+ 1) `~~MaxCount = 1,` \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Creating Instances (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`~~KeyName = keyfile\_name,` \newline % Row Count 1 (+ 1) `~~SecurityGroupIds = security\_group\_ids,` \newline % Row Count 3 (+ 2) `~~InstanceType = instance\_type,` \newline % Row Count 4 (+ 1) `~~BlockDeviceMappings = {[}` \newline % Row Count 5 (+ 1) `~~~~\{` \newline % Row Count 6 (+ 1) `~~~~'DeviceName': '/dev/sda1',` \newline % Row Count 8 (+ 2) `~~~~'Ebs': \{` \newline % Row Count 9 (+ 1) `~~~~~~'VolumeSize': disk\_size,` \newline % Row Count 11 (+ 2) \seqsplit{`~~~~~~'DeleteOnTermination':} True,` \newline % Row Count 13 (+ 2) `~~~~~~'VolumeType': 'gp2', ` \newline % Row Count 15 (+ 2) `~~~~\}` \newline % Row Count 16 (+ 1) `~~{]},` \newline % Row Count 17 (+ 1) `~~IamInstanceProfile = \{` \newline % Row Count 18 (+ 1) `~~~~'Arn': arn\_profile` \newline % Row Count 19 (+ 1) `~~\},` \newline % Row Count 20 (+ 1) `~~SubnetId = subnet\_id,` \newline % Row Count 21 (+ 1) `~~UserData = user\_data` \newline % Row Count 22 (+ 1) `)`% Row Count 23 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}