Having fun with Boto 3

Boto3 is a kick ass AWS client for python. If you are working as a DevOps Engineer with Cloud that's a library you must check it out. Boto3 improved a lot the design in comparison with boto2.

For this blog post, I will show how to do 2 simple things but yet very powerful things. Besides boto, I will show how to use Paramiko another cool library in Python which can manipulate SSH and connect to boxes and perform commands.

You will need to have an AWS account and you SECRETS in hand in order to make the code work. You can use python 2 or python 3 both can work with boto3.

Installing Boto3

For Python 2:

$ sudo pip install boto3

For Python 3:

$ sudo pip3 install boto3

List all EC2 Instances

These keys are set explicitly just for a sake of simplicity you should omit they and run an aws-configure with the AWS-CLI. AWS-CLI uses boto under the hood. It's possible to Filter by TAGS as well.

Connet to a BOX via SSH and run a Command

Paramiko needs to have the full path for the PEM file you use to create the box. Here I'm just running the Linux command hostname to get the name of the box. However, we could run complex bash script or anything you like it. We could combine both scripts to connect to all boxes.

This is very useful because easily we can create a central checker that can check for anything in all running instances we have in an EC2 Region. Boto3 has API for killing and creating instances too, so we could kill a instance if does not a match a specific requirement of Env settings.

Cheers,
Diego Pacheco


Popular posts from this blog

Telemetry and Microservices part2

Installing and Running ntop 2 on Amazon Linux OS

Fun with Apache Kafka