How to create a Boto3 Session to Interact with AWS from Python? The boto3.Session class allows you to customize various aspects of the AWS service clients, such as specifying endpoint URLs, specifying a custom profile from the AWS credentials file, access key, secret key, and more. In this article, I will cover some common use cases for using Boto3 session.

Create Boto3 Session

In Boto3, a session is an object that stores configuration state, including AWS access key ID, secret access key, and session token. The boto3.Session class is used to create a session, and it provides a way to customize and manage the configuration settings for AWS service clients.


# Import os
import os

# Set environment variables
os.environ['AWS_ACCESS_KEY_ID'] = 'your_access_key'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'your_secret_key'

# Create session
session = boto3.Session()

In the example above, a Boto3 session is created by using the default constructor.

The boto3.Session class allows you to customize various aspects of the AWS service clients, such as specifying endpoint URLs, specifying a custom profile from the AWS credentials file, and more.

Using Boto3 Session to Customize Configuration

You can pass various parameters to the boto3.Session constructor to customize the configuration, such as specifying the AWS access key, secret key, and region.


# Creating a session with custom configuration
session = boto3.Session(
    aws_access_key_id='your_access_key',
    aws_secret_access_key='your_secret_key',
    region_name='your_region',
    profile_name='custom_profile',
    endpoint_url='https://custom.endpoint.url'
)

# Creating an S3 client using the session
s3_client = session.client('s3')

# List all S3 buckets
response = s3_client.list_buckets()
print('S3 Buckets:', response['Buckets'])

The session is then used to create an S3 client (s3), and the client is used to get the bucket list.

Using Multiple Profiles

If you have multiple profiles in your AWS credentials file, you can use the profile_name parameter to select a specific profile when creating a session.


# Using Multiple Profiles
session = boto3.Session(profile_name='your_profile')

Using Environment Variables

You can use environment variables to set AWS credentials, and the boto3.Session class will automatically use them. This is the same as the first example.


# Import
import os

# Set os level environment variables
os.environ['AWS_ACCESS_KEY_ID'] = 'your_access_key'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'your_secret_key'

# Create default boto3 Session
session = boto3.Session()

Switching Between Roles

If you’re assuming a role, you can use the sts client to assume the role and then create a session with the temporary credentials.


# Switching between roles
sts = boto3.client('sts')
assumed_role = sts.assume_role(RoleArn='arn:aws:iam::account-id-with-role:role/role-name', RoleSessionName='session-name')
session = boto3.Session(
    aws_access_key_id=assumed_role['Credentials']['AccessKeyId'],
    aws_secret_access_key=assumed_role['Credentials']['SecretAccessKey'],
    aws_session_token=assumed_role['Credentials']['SessionToken']
)

Using Boto3 sessions provides flexibility in managing AWS credentials and configurations, allowing you to create more customized AWS service clients based on your specific requirements.

Conclusion

In this article, you have learned what a Boto3 session is and how to create and use it to access the AWS resources. Also, you have seen a Python example of listing all S3 bucket names.

Happy Learning !!

Related Articles

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium