Demystifying Python SDKs: Client vs Resource

Yash Trivedi
3 min readNov 20, 2023

--

Boto3: Client vs Resource

Python SDKs (Software Development Kits) serve as indispensable tools, bridging the gap between your code and the vast array of services offered by cloud providers like AWS. Within the realm of AWS SDKs, two key concepts often come into play: clients and resources. Let’s unravel the mystery behind these terms and understand when to wield each in your Python scripts.

The Client: Unveiling Low-Level Power

Scenario: S3 Bucket Manipulation

When you find yourself working with services like Amazon S3, where operations involve precise control over buckets, objects, and permissions, the Boto3 client takes the spotlight. The client provides a low-level, direct interface to the AWS service API. This grants you granular control over requests and responses, perfect for scenarios where fine-tuned manipulation is key.

Example: Creating an S3 Bucket

import boto3

# Initialize the S3 client
client = boto3.client('s3')

# Create a new S3 bucket with specific configurations
response = client.create_bucket(
Bucket="example-bucket",
ACL="public-read",
CreateBucketConfiguration={
'LocationConstraint': 'us-west-2'
}
)

# Print the response
print(response)

In this scenario, the client shines by providing direct access to S3 API calls, offering control over every detail of the bucket creation process.

The Resource: Embracing Object-Oriented Abstraction

Scenario: EC2 Instance Management

Now, consider the realm of Amazon EC2, where you deal with higher-level entities like instances, key pairs, and security groups. This is where the Boto3 resource steps onto the stage, offering a more Pythonic and object-oriented approach. The resource abstracts away some of the complexity, allowing you to interact with AWS services in a more intuitive manner.

Example: Launching an EC2 Instance

import boto3

# Initialize the EC2 resource
ec2_resource = boto3.resource('ec2')

# Launch an EC2 instance with specified parameters
response = ec2_resource.create_instances(
ImageId='ami-12345678',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
KeyName='my-key',
SecurityGroups=['my-security-group']
)

# Print the response
print(response)

In this scenario, the resource provides a higher-level, object-oriented interface, simplifying the process of managing EC2 instances.

Choosing Wisely: Client or Resource?

The decision between using a client or a resource boils down to the nature of the AWS service and the level of abstraction you desire in your Python script.

  • Client: Opt for the client when you need fine-grained control over AWS service operations, dealing with lower-level details and direct API interactions.
  • Resource: Embrace the resource when you seek a more Pythonic, higher-level abstraction, simplifying your code and focusing on the essential aspects of AWS service manipulation.

In the dynamic landscape of cloud development, understanding when to wield the power of a client or the elegance of a resource empowers you to craft Python scripts that are not only functional but also maintainable and expressive.

So, whether you’re sculpting S3 buckets with precision or orchestrating EC2 instances with elegance, make an informed choice between the client and resource in your Python SDK (Boto3) journey.

Thanks for Reading!

--

--

Yash Trivedi

Associate Cloud Engineer at Rishabh Software, AWS and PCAP Certified.