How to create an AWS session using Boto3 library in Python?

When working with AWS services through Python, you need to establish a session first. An AWS session handles authentication and provides access to AWS services using the Boto3 library.

An AWS session can be created with default credentials or customized based on your specific requirements.

Prerequisites

Before creating an AWS session, set up authentication credentials. You can find these in the IAM console or create a credentials file manually at ~/.aws/credentials ?

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
aws_session_token = YOUR_SESSION_TOKEN
region = REGION_NAME

Install Boto3 using the following command ?

pip install boto3

Creating a Default Session

Use Session() with no parameters to create a session with default credentials ?

import boto3

def create_default_session():
    session = boto3.session.Session()
    return session

# Create and display the session
session = create_default_session()
print(session)
Session(region_name=None)

Creating a Customized Session

For customized sessions, pass specific parameters like access keys, tokens, and region information ?

import boto3

def create_customized_session(aws_access_key, aws_secret_key, aws_token, 
                             region_name=None, profile_name=None):
    session = boto3.session.Session(
        aws_access_key_id=aws_access_key,
        aws_secret_access_key=aws_secret_key,
        aws_session_token=aws_token,
        region_name=region_name,
        profile_name=profile_name
    )
    return session

# Example usage (replace with your actual credentials)
session = create_customized_session(
    aws_access_key="YOUR_ACCESS_KEY",
    aws_secret_key="YOUR_SECRET_KEY", 
    aws_token="YOUR_SESSION_TOKEN",
    region_name="us-east-1"
)
print(session)
Session(region_name=us-east-1)

Session Parameters

The customized session accepts the following parameters ?

  • aws_access_key_id (string) − AWS access key ID
  • aws_secret_access_key (string) − AWS secret access key
  • aws_session_token (string) − AWS temporary session token
  • region_name (string) − Default region when creating new connections
  • profile_name (string) − The name of a profile to use

Using Session with AWS Services

Once created, you can use the session to access AWS services ?

import boto3

# Create session
session = boto3.session.Session(region_name='us-east-1')

# Use session to create service clients
s3_client = session.client('s3')
ec2_resource = session.resource('ec2')

print(f"S3 Client: {s3_client}")
print(f"EC2 Resource: {ec2_resource}")
S3 Client: <botocore.client.S3 object at 0x...>
EC2 Resource: ec2.ServiceResource()

Conclusion

AWS sessions in Boto3 provide authenticated access to AWS services. Use default sessions for simple applications or customized sessions when you need specific credentials, regions, or profiles.

Updated on: 2026-03-25T18:07:42+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements