How To Configure Python Boto3 SDK For AWS

How To Configure Python Boto3 SDK For AWS

How to configure python boto3 SDK for AWS

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed how to install Python Boto3 SDK for AWS.

https://cloudaffaire.com/how-to-install-python-boto3-sdk-for-aws/

In this blog post, we will discuss how to configure python boto3 SDK for AWS.

Boto3 configuration:

There are two types of configuration data in boto3: credentials and non-credentials. Credentials include items such as aws_access_key_id, aws_secret_access_key, and aws_session_token. Non-credential configuration includes items such as which region to use or which addressing style to use for Amazon S3. The distinction between credentials and non-credentials configuration is important because the lookup process is slightly different. Boto3 will look in several additional locations when searching for credentials that do not apply when searching for non-credential configuration.

The mechanism in which boto3 looks for credentials is to search through a list of possible locations and stop as soon as it finds credentials. The order in which Boto3 searches for credentials is:

  • Passing credentials as parameters in the boto.client() or boto.Session().
  • Passing credentials as Environment variables
  • Passing credentials as Shared credential file (~/.aws/credentials)
  • Passing credentials using AWS config file (~/.aws/config)
  • Passing credentials as by assume Role provider
  • Passing credentials using Boto2 config file (/etc/boto.cfg and ~/.boto)
  • Passing credentials through Instance metadata service on an Amazon EC2 instance that has an IAM role configured.

Next, we are going to explain some of the configuration options using examples.

Prerequisites for this demo:

  • One EC2 AWS Linux 2 instance.
  • One IAM user with programmatic admin access.

Demo:

Passing credentials as parameters:

You can directly pass your AWS_ACCESS_KEY and AWS_SECRET_KEY as parameter in your python code using boto.client() or boto.Session().

Passing credentials as environment variables:

You can declared your aws access and secret key’s in the form of AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables and boto3 will automatically detect this environment variable and will use them to connect to your AWS infrastructure.

Passing credentials using AWS CLI config\credentials files:

If you have AWS CLI installed in the system, you can pass credentials using ~/.aws/config and ~/.aws/credentials files. Boto3 will automatically detect the credential files and use the credential to interact with your AWS infrastructure.

Hope you have enjoyed this article. In the next blog post, we will discuss boto3 resources and will create our 1st AWS resource using Boto3 SDK.

To get more details on Python Boto3, please refer below AWS documentation

https://boto3.amazonaws.com/v1/documentation/api/latest/index.html

 

Leave a Reply

Close Menu