I worked as a professional software engineer for many years. First I wrote C code and built it using make. Later I wrote Java code and built it originally using Ant and then Maven. I’d written lots of code which used the AWS Java SDK. At the other extreme, I’d written BASH shell scripts, including some which used the AWS CLI tools. I had never considered automating with Python and Boto3.
An organization was migrating to AWS and asked about automation and Infrastucture as Code. The Java approach was great for “Enterprise Applications”, but the build process introduced considerable turnaround time. The BASH approach provided instant feedback, but using the output from one command as the input to the next command was often awkward, and sometimes impossible.
Automating with Python and Boto3
A coworker suggested that I try automating with Python and Boto3. At first, I was horrified. A language which forced indentation to declare statement blocks? I thought that died with COBOL!
What I grew to love was the ability to write code without needing a build system. It was easy to call AWS APIs and use the results. I could find libraries to do almost anything that I needed. AWS Lambda supports Python.
I also realized that I always indent my Java code. The curly braces in Jave were pretty much redundant.
The Python Way
There were other things to get used to. I was informed that these were “The Python Way”.
The most significant of these was about exception handling. In Java, code was carefully constructed not to throw exceptions. In Python, exceptions were used as a form of flow control. The whole idea of the try … else block was frightening.
The Python way did make things more efficient. Instead of making a call to AWS to determine state, and then another call to perform an action, Python would using a single call. The try…else construct adds some elegance to the approach. It allows the statement that might cause the exception to be independent of the code to run if there was no exception.
An Example Python and Boto3 App
The Boto3 library allows Python to talk to AWS. The following program will use the credentials setup by the aws configure CLI tool to connect to the default AWS account and list all the Amazon Simple Storage Service (S3) buckets. Lines 4 & 6 do the real work:
from botocore.exceptions import ClientError
except ClientError as e:
print( "Error: " + str( e ) )
print( "Response: " + str( response ) )