Automating and Provisioning to Amazon AWS using boto3 SDK for Python 3
Automating and Provisioning to Amazon AWS using boto3 SDK for Python 3
Code written for boto3 Python3 SDK v.1.20
About the Book
Creation Date: 2021-10-01
Last Update: 2022-01-16 v. 16
This book covers the Amazon SDK for Python 3: boto3.
Across 128 pages, DIN-A4 full size, PDF only, DRM-free, it explains, since the beginning, how to create a CLI (Command Line Interface) application that uses Amazon's boto3 Python SDK to interact with Amazon AWS and do operations like:
- Pass different credentials to boto3 as text (not just having on the default file locations)
- Create new Instances
- Start, Stop a Instance
- Stop all the running instances
- Start all the stopped instances
- List Instances
- Using tags
- Create, Add, Remove, EBS Volumes
- Create S3 Buckets, upload files, download files, list objects on a bucket
- Add CloudWatch Alerts
- Create tables in DynamoDB and perform Inserts
- Also includes how to deploy AWS group of instances from Ansible, and install Apache to one of the group through an Ansible Playbook
...
Full Source Code included.
It's completely hands on, and most of the book is code and explanations related, so you'll be able to code your own Automations for Amazon and save many hours or investigation and try and error.
It teaches you all the stuff you need to Automate Operations against Amazon AWS with Python 3.
The programs are tested to work with Ubuntu Linux, but they should work on any Linux distro, on Mac and Windows. (with the exception of chmod which is a specific command for Linux and Mac, and one Bash sample for Ansible which uses grep, another specific utility for Linux)
Bundles that include this book
Table of Contents
- Index...........................................................................3
- Contact me.......................................................................6
- Receiving Free Updates.............................................................6
- If you’re not satisfied..............................................................6
- MT Notation.....................................................................7
- Why using Amazon AWS boto3......................................................9
- Installing Amazon AWS boto3.......................................................9
- Create the Credentials in Amazon AWS Console........................................15
- Enabling a Default VPC.........................................................17
- Configuring the Credentials........................................................18
- Our first Python 3 sample..........................................................18
- Error handling...................................................................20
- boto3 and None Objects.........................................................20
- Access to the Source Code showed in the book.........................................21
- Programming the Application.......................................................22
- Main file: assignment.py........................................................22
- Credential passwords.txt file........................................................26
- The MenuUtils class..............................................................26
- Class KeyboardInputManagement...................................................29
- Starting the program..............................................................30
- Working with EC2................................................................30
- Code lib/awsec2.py.............................................................31
- Listing all the instances.........................................................39
- Create an instance based on AMI: create_instance_asking().............................41
- Stop a Running instance.........................................................49
- Start a stopped instance.........................................................50
- Create a new AMI Image based on an Instance.......................................51
- Amazon boto3 Resources and Clients.................................................52
- Client example: Creating Key Pairs................................................52
- Starting all the stopped Instances..................................................53
- Stopping all the Running Instances................................................53
- Working with EBS (Elastic Block Storage)............................................54
- Create a new Volume, with a Waiter................................................54
- The code: ec2_client.create_volume()..............................................55
- Prefix for Volumes: vol-.........................................................57
- Listing EBS Volumes...........................................................57
- The code for listing volumes.....................................................57
- The code for get_all_volumes(s_filter_instance_id="").................................58
- The boto3 resources call to get the volumes..........................................59
- Attach an existing Volume to an Instance............................................60
- Code for attaching a Volume to an Instance..........................................67
- Typical problems attaching Volumes...............................................70
- Detaching Volumes.............................................................70
- Useful scenarios of detaching a Volume.............................................70
- List own Snapshots.............................................................70
- The code for ec2_client.describe_snapshots()........................................71
- Taking a Snapshot of a Volume...................................................73
- Creating a Volume from a Snapshot................................................73
- S3 Storage......................................................................75
- Create a Bucket................................................................75
- The code for: ec2_s3_client.create_bucket().........................................76
- List Buckets..................................................................77
- Upload a File to S3.............................................................78
- List all objects in a Bucket.......................................................79
- Download an object from a Bucket................................................81
- Delete an Object from a Bucket...................................................84
- CloudWatch.....................................................................86
- Create an Alarm..................................................................87
- List Metrics...................................................................89
- Retrieve a specific Metric (CPUUtilization).........................................90
- Retrieve a specific Metric (LogicalDisk % Free Space)................................99
- Create an Alarm with Action enabled, that will stop the instance when the CPU load is greater
- than 60%....................................................................100
- Using Amazon DynamoDB........................................................102
- Create a table in DynamoDB (with Waiter).........................................103
- Counting items on a Table......................................................105
- Querying for a Table Creation Time...............................................106
- Inserting Data (put)............................................................107
- Delete a Table................................................................115
- Ansible..........................................................................116
- Prerequisites...................................................................116
- Amazon’s AWS...............................................................116
- Selection of the Image for the Master Node: Ubuntu 20.04 LTS.........................117
- Installation of the Master node in Virtual Box.......................................117
- Commands to install ansible in the Master Node in Ubuntu 20.04 LTS...................119
- Testing ansible (outside the program)................................................120
- Specifying Region (as an error showed, how I troubleshot)............................121
- Specifying AWS Credentials....................................................122
- Installing the ansible assignment program............................................122
- Running ansible.py..............................................................123
- How it works internally?..........................................................123
- The scripts can be executed independently from command line.........................125
- Problems (solved) with VPCs....................................................127
- Doing all the sequence with the application...........................................132
- How we control having two groups of instances?....................................133
- Is better demoed with a video :)..................................................134
- Later I added the task to list all the instance public Ips................................135
- Installing Apache to the Deployed Instances (first version)...............................136
- How I solved the problem with the Key Certificate...................................137
- How I solved promoting user ubuntu to root........................................140
- Final success.................................................................143
- How to test that Apache works.....................................................145
- Provisioning AWS EC2 Instances with Ansible and Automating Apache deployment with or without
- using Ansible Dynamic Inventory from Ubuntu 20.04 LTS...............................146
- Pre-requisites................................................................146
- If you want to use Dynamic Inventory.............................................146
- If you don’t want to use Dynamic Directory........................................149
- Alternative way parsing with awk and grep......................................150
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them