How To Manage AWS EC2 Infrastructure With Python

In this article, we’ll take a look at how system administrators can use Python to manage their Amazon Web Services (AWS)  Elastic Compute Cloud (EC2) infrastructure. You’ll learn to query existing EC2 instances, change the state of the virtual machines and their configurations, or terminate any instances that are no longer required.

Before we can proceed, make sure to setup your Python scripting environment for the first use, by referring my previous article here.

List All AWS EC2 Instances

Now that we’ve set up the scripting environment and configured our credentials, let’s begin by importing the boto3 library and using it to create an EC2 resource, which acts like a connection to access to AWS EC2 console. I’ll use this EC2 resource to iterate through all EC2 instances, then access the individual properties of each virtual machine and print the list of all EC2 instances and their respective properties on the console.

import boto3
 ec2 = boto3.resource('ec2')
 for instance in ec2.instances.all():
         "Id: {0}\nPlatform: {1}\nType: {2}\nPublic IPv4: {3}\nAMI: {4}\nState: {5}\n".format(, instance.platform, instance.instance_type, instance.public_ip_address,, instance.state

My new book :  PowerShell Scripting Guide to Python

This PowerShell Scripting guide to Python is designed to make readers familiar with syntax, semantics and core concepts of Python language, in an approach that readers can totally relate with the concepts of PowerShell already in their arsenal, to learn Python fast and effectively, such that it sticks with readers for longer time.

“Use what you know to learn what you don’t. ” also known as Associative learning.

Book follows a comparative method to jump start readers journey in Python, but who is the target audience? and who should read this book –

  • Any System Administrator who want to step into Development or Programming roles, and even if you don’t want to be a developer, knowledge of another scripting language will make your skill set more robust.
  • Python Developers who want to learn PowerShell scripting and understand its ease of user and importance to manage any platform.

Python is one of the top programming languages and in fast changing IT scenarios to DevOps and Cloud to the future – Data ScienceArtificial Intelligence (AI) and Machine Learning Python is a must know.

But this PowerShell Scripting guide to Python would be very helpful for you if you already have some knowledge of PowerShell

NOTE! This is a Leanpub “Agile-published” book. That means the book is currently unfinished and in-progress. As I continue to complete the chapters, we will re-publish the book with the new and updated content. Readers will receive an email once a new version is published!

While the book is in progress, please review it and send any feedback or error corrections at [email protected]

Optical Character Recognition

Subscribe to our mailing list

* indicates required