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

More Python and Boto3 - Arguments and Sessions

 2017-05-03 /  723 words /  4 minutes

In “Automating with Python and Boto3” the Python script had no special code for authentication or region selection. It simply uses defaults like those setup with the aws configure CLI command, or environment variables. Sooner of later though, there is a need to work in a different region, or handle multiple accounts, so the defaults aren’t enough. Fortunately, Boto3 supports alternatives for region selection and credentials.

Command Line Arguments with Python and Boto3

Python has a great library for managing command line parameters. You simply define the arguments that you want to parse, and then call the parser. The values associated with the arguments are stored in a map.

A couple of critical points to remember. First, arguments which contain a dash (“-“) are changed in the map to an underscore (“_”). Second, you don’t need to declare “-h” or “—help” arguments for displaying help. In fact, if you do you’ll get a conflict error. For example, the following program:

1 2 3 4 5 6 7 import argparse

parser = argparse.ArgumentParser( description=‘Test Command Line Parser’ ) parser.add_argument( ‘–help’, help=‘Provides command line help’ )

args = parser.parse_args() print( args ) will cause the error:

argparse.ArgumentError: argument -h/–help: conflicting option string(s): –help

There are lots of ways to determine the credentials to use. I prefer to use profiles to select between the different accounts. Although profiles can be configured with a default region, I like to allow a region to be specified on the command line. I typically like to support arguments like:

$ python args.py –help usage: args.py [-h] [–profile PROFILE] [–region REGION]

AWS Utility

optional arguments: -h, –help show this help message and exit –profile PROFILE The AWS Profile to use –region REGION The AWS Region 1 2 3 4 5 6 7 8 9 $ python args.py –help usage: args.py [-h] [–profile PROFILE] [–region REGION]

AWS Utility

optional arguments: -h, –help show this help message and exit –profile PROFILE The AWS Profile to use –region REGION The AWS Region The code to generate this help text is:

import argparse

parser = argparse.ArgumentParser( description=‘AWS Utility’ ) parser.add_argument( ‘–profile’, help=‘The AWS Profile to use’, default=“default” ) parser.add_argument( ‘–region’, help=‘The AWS Region’ )

args = parser.parse_args() print( ‘Profile: %s’ % args.profile ) print( ‘Region: %s’ % args.region ) 1 2 3 4 5 6 7 8 9 import argparse

parser = argparse.ArgumentParser( description=‘AWS Utility’ ) parser.add_argument( ‘–profile’, help=‘The AWS Profile to use’, default=“default” ) parser.add_argument( ‘–region’, help=‘The AWS Region’ )

args = parser.parse_args() print( ‘Profile: %s’ % args.profile ) print( ‘Region: %s’ % args.region ) Sessions and Clients with Python and Boto3 Once we know the profile and region to use we need to tell Boto3 to use them. This is done using Boto3 Sessions. Boto3 Sessions define a connection to AWS. A single Python program can have multiple sessions open concurrently.

Sessions don’t provide a way to access AWS Services. Clients provide an interface to one AWS Service by supporting methods which call AWS. Boto3 Clients are created from the session.

The following is an example program with support profile and region selection. It provides the same functionality as the example program from “Automating with Python and Boto3“. Lines 13 and 14 create the Boto3 Session which supports multiple profiles and multiple regions:

import argparse import boto3 from botocore.exceptions import ClientError

parser = argparse.ArgumentParser( description=‘AWS Utility’ ) parser.add_argument( ‘–profile’, help=‘The AWS Profile to use’, default=“default” ) parser.add_argument( ‘–region’, help=‘The AWS Region’ )

args = parser.parse_args() print( ‘Profile: %s’ % args.profile ) print( ‘Region: %s’ % args.region )

session = boto3.session.Session( region_name=args.region, profile_name=args.region ) s3client = session.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 11 12 13 14 15 16 17 18 19 20 import argparse import boto3 from botocore.exceptions import ClientError

parser = argparse.ArgumentParser( description=‘AWS Utility’ ) parser.add_argument( ‘–profile’, help=‘The AWS Profile to use’, default=“default” ) parser.add_argument( ‘–region’, help=‘The AWS Region’ )

args = parser.parse_args() print( ‘Profile: %s’ % args.profile ) print( ‘Region: %s’ % args.region )

session = boto3.session.Session( region_name=args.region, profile_name=args.region ) s3client = session.client( ‘s3’ ) try: response = s3client.list_buckets() except ClientError as e: print( “Error: " + str( e ) ) else: print( “Response: " + str( response ) )


Tags:  Automation  Python  Boto3
Categories:  Automation  Python

See Also

 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.