Logo

AWS cloud automation with Python and Boto

Creating a connection class for our Python and Boto library

This is a continuation of Getting started with Python and boto. You can read this article on its own, or if you need some context you can also take a peak to the previous article.

Our objective here is to learn Python and boto3, and in the process organize a custom, small and opinionated Python library, based on boto3 that you can use as a starting point to automate creating and managing AWS cloud resources. As we'll take an OOP approach, we will need a class to deal with connecting to AWS. We will name this class Connection and we will place it in a hello_connection.py file.

As a word of advice, use the code that follows in our articles as an inspiration source, and not as the right or only way to do it. Tweak the code in a way that make sense for you, learn from it if there's anything to learn and feel free to steal ideas or contribute with ideas.

We're choosing here to make a Connection class that will receive the AWS credentials in the constructor, and the region.

Then when a new object of this class is made, the session property on this object will actually return a configured boto3 session. As each AWS service has a different name and a different boto3 client object that can be used to work with that AWS service, we create one client for each AWS service we'll use frequently in this Connection class.

So to recap, the final objective of our Connection class is to be able to have our AWS connection configured, and connect easily to each AWS service.

For now, we'll include a code in the if __name__ == '__main__' fragment, to quickly test our connection class. Our code then becomes: