All about Cloud, mostly about Amazon Web Services (AWS)

Automating with Python and Boto3

 2017-03-25 /  494 words /  3 minutes

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 enterprise applications 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. Python provided a good compromise.

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 Java 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:

Simple Python Boto3 ScriptPython import boto3 from botocore.exceptions import ClientError

s3client = boto3.client( ‘s3’ ) try: response = s3client.list_buckets() except ClientError as e: print( “Error: " + str( e ) ) else: print( “Response: " + str( response ) ) 1 2 3 4 5 6 7 8 9 10 import boto3 from botocore.exceptions import ClientError

s3client = boto3.client( ‘s3’ ) try: response = s3client.list_buckets() except ClientError as e: print( “Error: " + str( e ) ) else: print( “Response: " + str( response ) )


Tags:  Automation  Python  Boto3  SDK
Categories:  AWS  AWS SDK

 Top Ten Tags

AWS (43)   Kinesis (9)   Streams (8)   AWS Console (5)   Go (5)   Analytics (4)   Data (4)   database (4)   Amazon DynamoDB (3)   Amazon Elastic Compute Cloud (EC2) (3)  


All Tags (173)

Disclaimer

All data and information provided on this site is for informational purposes only. cloudninja.cloud makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis.

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. My opinions may change over time.