What Is the Difference Between Boto3 Resource, Client, and Session?

Introduction

Boto3 is an AWS SDK for Python. It provides object-oriented API services and low-level services to the AWS services. It allows users to create, and manage AWS services such as EC2 and S3.

There are three main objects in Boto3 that are used to manage and interact with AWS Services. Namely Session, Client, and resource.

In this tutorial, you’ll learn

  • What is the difference between AWS Boto3 session, client and resource
  • What is AWS session, client and resources
  • How to create them using the AWS Credentials directly or using Environment variables

Boto3 Session vs Boto3 Client vs Boto3 Resource

SessionClientResource
Create service clients and resourcesLow-level AWS service ClassHigh level, object-oriented API
A session is an object to create a connection to AWS Service and manage the state of the connectionIt provides methods to connect with AWS services similar to the AWS API service.It represents the Object-oriented interface to AWS services.
All API services are available in the Boto3 Client. Maps 1:1 with the AWS service APINot all API services are available in the resource. It has actions() defined to make calls to the AWS services.
Uses Snake case notation for the naming conventions. e.g.: list_buckets()Uses UpperCaseCamelCase notation for the naming convention. Because the resource is similar to classes. Python classes follow this notation.

When you want to use the environment variables for specifying the AWS credentials, you should have already configured the AWS CLI in your machine. The configuration will be stored in the location ~/.aws/credentials.
To know more about how to install and Configure AWS client, read How to Install AWS Cli on Ubuntu and Configure AWS Cli?

Boto3 Session

Boto3 session is an object to create a connection to your AWS service and manage the connection state throughout your program life cycle.

Create Boto3 Session

You can create Boto3 session using your AWS credentials Access key id and secret access key.

Creating a Boto3 Session by Directly Specifying the Credentials

In the section, you’ll directly specify the AWS credentials for creating a boto3 session.

Boto3 Session Example

#Creating Session With Boto3.
session = boto3.Session(
aws_access_key_id='Your Access Key ID',
aws_secret_access_key='You Secret access key'
)

Creating a Boto3 Session by Using ENVIRONMENT Variables

In this section, you’ll create a Boto3 session by using the environmental variables for specifying the AWS Credentials.

Boto3 Session Example

#Creating Session With Boto3.
session = boto3.Session(
aws_access_key_id=settings.AWS_SERVER_PUBLIC_KEY,
aws_secret_access_key=settings.AWS_SERVER_SECRET_KEY
)

Boto3 Client

Boto3 client is a low-level service class to connect to AWS service. It provides similar methods available in the AWS API. All the methods available in the AWS API are available in the Boto3 client.

Create Boto3 Client

You can create AWS Boto3 client using your AWS credentials Access key id and secret access key.

Creating a Boto3 Client by Directly Specifying the Credentials

In the section, you’ll directly specify the AWS credentials for creating a boto3 client.

Boto3 Client Example

s3_client = boto3.client('s3', 
                      aws_access_key_id='Your Access Key ID', 
                      aws_secret_access_key='You Secret access key' 
                      region_name='YOUR_REGION_NAME'
                      )

Creating a Boto3 Client by Using ENVIRONMENT Variables

In this section, you’ll create Boto3 client by using the environmental variables for specifying the AWS Credentials.

You can use the below code to create a Boto3 client.

Boto3 Client Example

s3_client = boto3.client('s3', 
                      aws_access_key_id=settings.AWS_SERVER_PUBLIC_KEY, 
                      aws_secret_access_key=settings.AWS_SERVER_SECRET_KEY, 
                      region_name=REGION_NAME
                      )

Boto3 Resource

Boto3 resource is a high-level object-oriented API service you can use to connect and access your AWS resource. It has actions() defined which can be used to make calls to the AWS service.

Create a Boto3 Resource

You can create a Boto3 resource using the session you’ve already created. In this section, assume that you have created a session to Boto3 in the variable called session.

Now, you’ll create a Boto3 resource with the session.

Resource() accepts the AWS service name as a parameter. In the below example, you’ll create an S3 resource that can be used to perform operations in your S3 objects.

Boto3 Resource Example

s3 = session.resource('s3')

Conclusion

In this tutorial, you’ve learned the difference between Boto3 resource, client, and session.

You have also learned how to create Boto3 resources, clients, and sessions directly by specifying the AWS credentials or using the AWS configuration environment variables.

What Next?

You may want to install pip3 on Ubuntu. Follow the below guide.

How to Install Pip3 on Ubuntu 20.04 and Use It?

Leave a Comment