What is the difference between Boto3 resource, client, and session? Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python. Boto3 provides an easy-to-use API for interacting with AWS services using Python code.

Advertisements

Boto3.Session manages AWS configuration, including credentials and region. Boto3.Client offers a lower-level, service-specific interface for direct API calls to AWS services, providing fine-grained control. Boto3.Resource is a higher-level, object-oriented abstraction for interacting with AWS services, offering a more Pythonic interface. Session configures settings, Client executes service-specific actions, and Resource provides an abstracted, object-oriented interaction with AWS resources, balancing flexibility and convenience in the Boto3 Python SDK.

What is Boto3 Resource, Client, Session

boto3.client is more suitable when you need fine-grained control over the API requests and responses, and when you want to closely follow the structure of the AWS service API. Whereas boto3.resource provides a higher-level, object-oriented, and abstracted interface for working with AWS resources,

The choice between client and resource depends on your specific use case and the level of abstraction and control you require when interacting with AWS services.

Boto3 Session

In Boto3, a boto3.Session is an object that stores configuration state, including AWS access key ID, secret access key, session token, and other settings. It provides a way to manage and customize the configuration settings for AWS service clients. The Session class is often used as a central point for configuring AWS credentials and other parameters that are shared among multiple clients and resources.

Boto3 Client

In Boto3, the boto3.client is a lower-level interface for making direct API calls to AWS services. It provides a more direct and service-specific way to interact with AWS services compared to the higher-level boto3.resource. The Client class is used to create service clients for different AWS services, such as S3, EC2, DynamoDB, etc.

Key features and characteristics of Boto3 Client:

  • Each service client (Client) has methods that directly map to AWS service API actions. For example, an S3 client might have methods like list_buckets(), put_object(), get_object(), etc.
  • With Client, you have more control over the details of API requests and responses. You directly invoke API actions with explicit parameters.
  • Requires explicit passing of credentials (access key, secret key), unless the credentials are available from other sources (environment variables, configuration files, IAM roles, etc.).
  • Client provides direct access to features and parameters specific to the underlying AWS service. It mirrors the AWS service API closely.
  • For operations that can return a large number of results, such as listing objects in an S3 bucket, you need to handle pagination explicitly.

Boto3 Resource

In Boto3, the boto3.resource is a higher-level, more Pythonic interface for interacting with AWS services. It provides an object-oriented approach to working with AWS resources, abstracting away many of the low-level details of the service API. The Resource class allows you to interact with AWS resources in a more intuitive and natural way compared to using the lower-level Client interface.

Here are some key features and characteristics of Boto3 Resource:

  1. boto3.resource provides an object-oriented interface, allowing you to work with AWS resources as Python objects.
  2. It abstracts away many of the complexities of the underlying AWS service API, making it easier to work with resources in a Pythonic manner.
  3. Resource methods that return multiple items (e.g., listing objects in an S3 bucket) automatically handle pagination, simplifying code for large result sets.
  4. The Resource class automatically inherits credentials from the associated Boto3 session or uses default credentials if none are explicitly provided.
  5. Resource methods often reflect the lifecycle of the AWS resource they represent. For example, methods for S3 buckets may include create(), delete(), and others.
  6. Resource allows for customization and extension, making it possible to create more specialized interfaces for specific use cases or services.

While boto3.client provides a lower-level, service-specific API for direct interactions with AWS services, boto3.resource is designed to offer a more abstracted, Pythonic, and object-oriented approach to working with AWS resources. The choice between using Boto3 client or resource depends on the level of abstraction and convenience you need for your specific use case.

Create Boto3 Resource, Client, and Session

Since the Resource, Client, and Session are objects in Boto3, let’s see the different ways of creating Boto3 Resource, Client, and Session objects in Python.

Boto3 Session Example

Following is an example of the Boto3 session, which is used to set the configuration to the boto3 session.


# Using Session
import boto3

# Creating a Boto3 session with specific credentials and region
session = boto3.Session(
    aws_access_key_id='your_access_key',
    aws_secret_access_key='your_secret_key',
    region_name='your_region'
)

Boto3 Client Example

Following is an example of a Boto3 client to get the S3 buckets and display them to the console.


# Using Client
import boto3

# Create an S3 client
s3_client = boto3.client('s3')

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

In this example, the s3_client is created using boto3.client('s3'), and then it is used to make an API call (list_buckets) to get all S3 buckets.

Boto3 Resource Example

Following is an example of a Boto3 resource to get the S3 buckets and display them to the console.


# Using Resource
import boto3

# Create an S3 resource
s3_resource = boto3.resource('s3')

# List all S3 buckets
for bucket in s3_resource.buckets.all():
    print('S3 Bucket:', bucket.name)

In this example, the s3_resource is created using the boto3.resource interface, and then it is used to iterate over all S3 buckets using the buckets.all() method.

Difference between Boto3 Resource vs Client vs Session?

Boto3 Session is often used to create AWS service clients and resources. Clients and resources created from the same session share the same configurations.


# Using the session to create an S3 client
s3_client = session.client('s3')

# Using the session to create an S3 resource
s3_resource = session.resource('s3')

Below are the features and differences between Boto3 Session, Client and Resource in table format.

Featureboto3.Sessionboto3.clientboto3.resource
PurposeManages configuration for AWS services,Provides a low-level interface to AWSProvides a higher-level, more Pythonic
CreationCreated using boto3.Session()Created using session.client('service')Created using session.resource('service')
Configuration OptionsAllows configuration of credentials,Allows configuration of credentials,Allows configuration of credentials,
UsageUsed to create service clients and resources with consistent configuration.Used for low-level interactions with AWS services using service-specificUsed for higher-level interactions with AWS services using resource-specific
CredentialsCan be used to explicitly pass credentials, or it inherits from defaultRequires explicit passing of credentials or inherits from the default credentials.Inherits credentials from the associated session or uses default credentials
Service-Specific MethodsDoes not have service-specific methods.Has service-specific methods that map to AWS service API actions.Has service-specific methods that map to AWS service API actions.
Object OrientationPrimarily used for configuration and creating clients/resources.Lower-level and closely mirrors AWS service APIs.Higher-level, object-oriented, and abstracts underlying complexities.
Boto3 resource vs client vs session

Conclusion

In this article, you have learned what a boto3 session, client, and resource is. And you also learned each topic with examples. In summary Boto3.Resource is a higher-level, object-oriented abstraction for interacting with AWS services, offering a more Pythonic interface. Session configures settings, Client executes service-specific actions, and Resource provides an abstracted, object-oriented interaction with AWS resources, balancing flexibility and convenience in the Boto3 Python SDK.

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