Boto3 Resource, Client and Session

Boto3 Core Concepts Explained

Published on Jul 15, 2021

Reading time: 2 minutes.


Core concepts of Boto3


  • There are few concepts of boto3 (aws sdk) like Session, Resource, Client, Meta, Collections, Paginators
  • In this post we will discuss about the kehy difference between Resource, Client and Session

Session


A session manages state about a particular configuration. By default, a session is created for you when needed. However, it’s possible and recommended that in some scenarios you maintain your own session. Sessions typically store the following:

  • Credentials
  • AWS Region
  • Other configurations related to your profile

Default session

Boto3 acts as a proxy to the default session. This is created automatically when you create a low-level client or resource client

1
2
3
4
5
6
7
8
import boto3

# Using the default session
s3 = boto3.resource('s3')

for each_buck in s3.buckets.all():
    print(each_buck.name)

Custom session

You can also manage your own session and create low-level clients or resource clients from it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import boto3
import boto3.session

# Create your own session
my_session = boto3.session.Session(profile_name=None) #profile_name is none as in we are going with default user in the .aws/credentials. 


# Now we can create low-level clients or resource clients from our custom session
s3 = my_session.resource('s3')

for each_buck in s3.buckets.all():
    print(each_buck.name)

Session configurations

You can configure each session with specific credentials, AWS Region information, or profiles. The most common configurations you might use are:

1
boto3.session.Session(aws_access_key_id=None, aws_secret_access_key=None, aws_session_token=None, region_name=None, botocore_session=None, profile_name=None)
  • 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
  • botocore_session (botocore.session.Session) – Use this Botocore session instead of creating a new default one.
  • profile_name (string) – The name of a profile to use. If not given, then the default profile is used.

Resource

Resources represent an object-oriented interface to Amazon Web Services (AWS). They provide a higher-level abstraction than the raw, low-level calls made by service clients. To use resources, you invoke the resource() method of a Session and pass in a service name

1
2
3
4
5
6
7
8
9
import boto3
import boto3.session

my_session = boto3.session.Session(profile_name=None) 
# Now we can create low-level clients or resource clients from our custom session
s3 = my_session.resource('s3') # you can use any resource within aws 

for each_buck in s3.buckets.all():
    print(each_buck.name)

Clients

Clients provide a low-level interface to AWS whose methods map close to 1:1 with service APIs. All service operations are supported by clients. Clients are generated from a JSON service definition file.

Diffeence between client and resource

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import boto3

print ("This is using resource")

my_session = boto3.session.Session(profile_name=None)
iam_resource = my_session.resource('iam')


for each_user in iam_resource.users.all(): #resource provides higher-level abstraction than the raw, low-level calls made by service clients
    print(each_user.name)

print ("-----------------------")

print ("This is using client")


my_session = boto3.session.Session(profile_name=None)
iam_client= my_session.client(service_name='iam',region_name='us-east-a')

for each in iam_client.list_users()['Users']:  #in client method we have to search for Users as Dict and with the output we have to playaround
    print (each['UserName'])