Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+365 -335
View File
@@ -1,349 +1,379 @@
---
description: This rule file outlines best practices for using the boto3 library, including code organization, security, performance, testing, and common pitfalls. It aims to ensure efficient, secure, and maintainable AWS integrations with boto3.
globs: *.py
description: This rule file guides developers on writing idiomatic, high-performance, and type-safe boto3 code, emphasizing client reuse, proper pagination, batch operations, and robust testing strategies.
globs: **/*.py
---
---
# boto3 Best Practices
This document outlines best practices for using the boto3 library in Python for interacting with Amazon Web Services (AWS). Following these guidelines will help you write more efficient, secure, and maintainable code.
Boto3 is the definitive Python SDK for AWS. Mastering it means writing code that is performant, maintainable, and resilient. This guide outlines the essential patterns and anti-patterns for modern `boto3` development.
## 1. Project Setup and Authentication
## 1. Client/Resource Management & Reuse
- **Use Sessions**: Always use boto3 sessions for managing configurations and credentials effectively.
python
import boto3
session = boto3.Session()
s3 = session.client('s3')
- **Avoid Hard-coding Credentials**: Never hard-code AWS credentials in your code. Use environment variables, AWS CLI configuration, IAM roles, or AWS Secrets Manager.
Never instantiate `boto3` clients or resources inside performance-critical loops or frequently called functions. Reusing client objects reduces credential churn, improves connection pooling, and significantly boosts performance.
- **Environment Variables**:
python
import os
aws_access_key_id = os.environ.get('AWS_ACCESS_KEY_ID')
aws_secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY')
s3 = boto3.client('s3', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
### ✅ GOOD: Module-level or Dependency Injected Clients
- **AWS CLI Configuration**:
Configure your credentials using the AWS CLI (`aws configure`). Boto3 will automatically pick up the credentials.
Instantiate clients/resources once per module or inject them via a wrapper class.
- **IAM Roles**: Use IAM roles for EC2 instances or Lambda functions to grant permissions.
- **AWS Secrets Manager**: Securely store and retrieve secrets (like database passwords or API keys) using AWS Secrets Manager, accessible via boto3.
## 2. Code Organization and Structure
- **Directory Structure**: Organize your project with a clear directory structure.
my_project/
├── src/
│ ├── __init__.py
│ ├── aws/
│ │ ├── __init__.py
│ │ ├── s3_utils.py
│ │ ├── dynamodb_utils.py
│ ├── models/
│ │ ├── __init__.py
│ │ ├── user.py
│ ├── main.py
├── tests/
│ ├── __init__.py
│ ├── aws/
│ │ ├── test_s3_utils.py
│ │ ├── test_dynamodb_utils.py
├── requirements.txt
├── .env
└── README.md
- **File Naming Conventions**: Use descriptive and consistent file names.
- `s3_utils.py`: Contains S3-related utility functions.
- `dynamodb_utils.py`: Contains DynamoDB-related utility functions.
- `user.py`: Defines a User model or data structure.
- **Module Organization**: Break down your code into logical modules.
- `aws/`: Contains modules for interacting with different AWS services.
- `models/`: Contains data models representing your application's entities.
- **Component Architecture**: Design your application with reusable components.
- Create separate functions or classes for different AWS operations (e.g., uploading files, querying DynamoDB).
- **Code Splitting**: Split large files into smaller, more manageable files.
- Group related functions and classes into separate files.
- Use clear and concise names for your files and modules.
## 3. Common Patterns and Anti-patterns
- **Design Patterns**: Use appropriate design patterns for boto3 interactions.
- **Factory Pattern**: Create clients using a factory pattern to abstract client creation.
python
class AWSClientFactory:
def create_client(self, service_name, session=None):
if not session:
session = boto3.Session()
return session.client(service_name)
factory = AWSClientFactory()
s3 = factory.create_client('s3')
- **Strategy Pattern**: Implement different strategies for handling AWS operations (e.g., different retry mechanisms).
- **Recommended Approaches**: Follow recommended approaches for common tasks.
- **Uploading Files to S3**: Use `upload_file` or `upload_fileobj` for uploading files to S3.
python
s3.upload_file('my_local_file.txt', 'my_bucket', 'my_s3_key.txt')
- **Downloading Files from S3**: Use `download_file` or `download_fileobj` for downloading files from S3.
python
s3.download_file('my_bucket', 'my_s3_key.txt', 'my_local_file.txt')
- **Anti-patterns**: Avoid common anti-patterns.
- **Tight Coupling**: Don't tightly couple your application logic with boto3 code. Abstract AWS interactions behind interfaces.
- **Ignoring Errors**: Always handle exceptions when interacting with AWS services.
- **Over-permissioning**: Grant only the necessary permissions to IAM roles.
- **State Management**: Manage state effectively in your boto3 applications.
- **Stateless Functions**: Prefer stateless functions that don't rely on global variables.
- **DynamoDB Sessions**: Use DynamoDB for storing session data in web applications.
- **Error Handling**: Implement robust error handling.
python
from botocore.exceptions import ClientError
try:
response = s3.get_object(Bucket='my_bucket', Key='my_key')
print(response['Body'].read().decode('utf-8'))
except ClientError as e:
if e.response['Error']['Code'] == 'NoSuchKey':
print('The specified key does not exist.')
else:
print(f'An error occurred: {e}')
## 4. Performance Considerations
- **Optimization Techniques**: Optimize boto3 interactions for performance.
- **Pagination**: Use pagination to handle large datasets.
python
paginator = s3.get_paginator('list_objects_v2')
pages = paginator.paginate(Bucket='my_bucket')
for page in pages:
for obj in page['Contents']:
print(obj['Key'])
- **Batch Operations**: Use batch operations for efficient data processing.
python
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket')
delete_keys = [{'Key': 'file1.txt'}, {'Key': 'file2.txt'}]
response = bucket.delete_objects(Delete={'Objects': delete_keys})
print(response)
- **Transfer Acceleration**: For faster uploads over long distances, enable S3 Transfer Acceleration.
python
s3 = boto3.client('s3',
config=boto3.session.Config(
s3={'use_accelerate_endpoint': True}
)
)
- **Memory Management**: Be mindful of memory usage when working with large files or datasets.
- **Streaming**: Use streaming operations to process data in chunks.
- **Garbage Collection**: Ensure proper garbage collection to release unused memory.
- **Bundle Size Optimization**: Reduce bundle size by using only necessary boto3 modules.
- **Lazy Loading**: Load boto3 modules only when needed to reduce startup time.
## 5. Security Best Practices
- **Common Vulnerabilities**: Understand and prevent common vulnerabilities.
- **Credential Leakage**: Protect your AWS credentials from being exposed in code or logs.
- **Injection Attacks**: Sanitize inputs to prevent injection attacks.
- **Cross-Site Scripting (XSS)**: Protect against XSS attacks when displaying data from AWS.
- **Input Validation**: Validate all inputs to prevent security issues.
- **Sanitize Inputs**: Remove or escape potentially harmful characters from inputs.
- **Use Parameterized Queries**: Use parameterized queries to prevent SQL injection attacks when working with databases.
- **Authentication and Authorization**: Implement proper authentication and authorization mechanisms.
- **Principle of Least Privilege**: Grant only the necessary permissions to IAM roles and users.
- **Multi-Factor Authentication (MFA)**: Enable MFA for sensitive operations.
- **Data Protection**: Protect your data at rest and in transit.
- **Encryption**: Use server-side encryption (SSE-S3) for data security by default. For additional security using AWS Key Management Service (KMS) keys, include encryption parameters
python
response = boto3.client('s3').upload_file(
file_path,
bucket_name,
key,
ExtraArgs={
'ServerSideEncryption': 'aws:kms',
'SSEKMSKeyId': 'your-kms-key-id'
}
)
- **Secure Transport**: Enforce HTTPS-only access by applying a bucket policy.
- **Versioning**: Enable S3 Versioning for recovery.
- **Secure API Communication**: Ensure secure communication with AWS APIs.
- **HTTPS**: Always use HTTPS for communication with AWS services.
## 6. Testing Approaches
- **Unit Testing**: Write unit tests for your boto3 components.
- **Mocking**: Use mocking libraries like `moto` to simulate AWS services during testing.
- **Test Cases**: Create comprehensive test cases for different scenarios.
- **Integration Testing**: Test the integration of your boto3 components with AWS services.
- **LocalStack**: Use LocalStack to simulate AWS services locally for integration testing.
- **End-to-End Tests**: Write end-to-end tests to verify the complete application flow.
- **Test Organization**: Organize your tests in a clear and maintainable structure.
- **Separate Test Files**: Create separate test files for each module or component.
- **Descriptive Test Names**: Use descriptive names for your test functions and classes.
- **Mocking and Stubbing**: Use mocking and stubbing techniques to isolate components during testing.
- **Mock Boto3 Clients**: Mock boto3 clients to avoid making real API calls during unit tests.
python
import boto3
from moto import mock_aws
import unittest
class MyTestCase(unittest.TestCase):
@mock_aws
def test_s3_upload(self):
s3 = boto3.client('s3')
s3.create_bucket(Bucket='mybucket')
s3.put_object(Bucket='mybucket', Key='myfile', Body='my content')
response = s3.get_object(Bucket='mybucket', Key='myfile')
self.assertEqual(response['Body'].read().decode('utf-8'), 'my content')
if __name__ == '__main__':
unittest.main()
## 7. Common Pitfalls and Gotchas
- **Frequent Mistakes**: Be aware of common mistakes when using boto3.
- **Incorrect Region**: Specifying the wrong AWS region.
- **Missing Permissions**: Insufficient IAM permissions.
- **Not Handling Exceptions**: Ignoring exceptions raised by boto3.
- **Edge Cases**: Consider edge cases when designing your application.
- **Throttling**: Handle API throttling by implementing retry mechanisms.
- **Version-Specific Issues**: Be aware of version-specific issues with boto3.
- **API Changes**: Keep up with API changes and deprecations.
- **Compatibility**: Test your code with different boto3 versions.
- **Compatibility Concerns**: Address compatibility concerns between boto3 and other technologies.
- **Python Versions**: Ensure compatibility with different Python versions.
- **Dependency Conflicts**: Resolve dependency conflicts with other libraries.
- **Debugging**: Use effective debugging strategies for boto3 applications.
- **Logging**: Implement detailed logging to track API calls and errors.
- **Debugging Tools**: Use debugging tools like `pdb` or IDE debuggers.
## 8. Tooling and Environment
- **Recommended Development Tools**: Use recommended development tools.
- **IDE**: Use a Python IDE like VS Code, PyCharm, or Sublime Text.
- **AWS CLI**: Use the AWS CLI for managing AWS resources.
- **Build Configuration**: Configure your build process for boto3 projects.
- **Requirements File**: Use a `requirements.txt` file to manage dependencies.
- **Virtual Environments**: Use virtual environments to isolate dependencies.
- **Linting and Formatting**: Follow linting and formatting recommendations.
- **PEP 8**: Adhere to PEP 8 style guidelines.
- **Linters**: Use linters like `flake8` or `pylint` to enforce code quality.
- **Formatters**: Use formatters like `black` to automatically format your code.
- **Deployment**: Follow deployment best practices.
- **Infrastructure as Code (IaC)**: Use IaC tools like Terraform or CloudFormation to manage your infrastructure.
- **CI/CD**: Implement CI/CD pipelines for automated deployments.
- **CI/CD Integration**: Integrate your boto3 projects with CI/CD systems.
- **Automated Tests**: Run automated tests during the CI/CD process.
- **Deployment Pipelines**: Create deployment pipelines to automate the deployment process.
## 9. Advanced S3 features
- S3 Object Lambda: Process and transform the data returned by S3 GET requests on the fly without modifying the stored object.
- S3 Lifecycle policies: Automate the transition of objects to more cost-effective storage classes, or schedule their deletion based on defined rules.
- S3 Event Notifications: Monitor bucket events and trigger actions such as invoking Lambda functions, sending messages via SNS or SQS, when new objects are created.
For example, you can configure S3 Event Notifications with Boto3 as follows:
python
```python
# my_aws_service.py
import boto3
s3_client = boto3.client('s3')
notification_configuration = {
'LambdaFunctionConfigurations': [
{
'LambdaFunctionArn': 'arn:aws:lambda:us-east-1:123456789012:function:ProcessS3Event',
'Events': ['s3:ObjectCreated:*'],
'Filter': {
'Key': {
'FilterRules': [
{'Name': 'suffix', 'Value': '.jpg'}
]
}
}
}
]
}
s3_client.put_bucket_notification_configuration(
Bucket='your-bucket-name',
NotificationConfiguration=notification_configuration
)
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_s3.client import S3Client
from mypy_boto3_s3.service_resource import S3ServiceResource
## 10. Best Practices Summary
# Module-level client/resource for simple cases
S3_CLIENT: S3Client = boto3.client("s3", region_name="us-east-1")
S3_RESOURCE: S3ServiceResource = boto3.resource("s3", region_name="us-east-1")
- **Security**: Prioritize security by avoiding hard-coded credentials, enforcing the principle of least privilege, and using encryption.
- **Performance**: Optimize performance by using pagination, batch operations, and S3 Transfer Acceleration.
- **Error Handling**: Implement robust error handling with comprehensive logging.
- **Testing**: Write unit tests, integration tests, and end-to-end tests using mocking and LocalStack.
- **Code Organization**: Maintain a clear and modular code structure.
class S3Manager:
"""Manages S3 operations with a reusable client."""
def __init__(self, s3_client: S3Client = S3_CLIENT):
self._s3_client = s3_client
By following these best practices, you can ensure that your boto3 applications are efficient, secure, and maintainable.
def list_my_buckets(self) -> list[str]:
response = self._s3_client.list_buckets()
return [b["Name"] for b in response.get("Buckets", [])]
# In another part of your application:
manager = S3Manager()
buckets = manager.list_my_buckets()
```
### ❌ BAD: Repeated Client Instantiation
Avoid creating new clients/resources for every operation.
```python
# my_aws_service_bad.py
import boto3
def get_bucket_names_bad() -> list[str]:
# This creates a new client every time the function is called
s3_client = boto3.client("s3", region_name="us-east-1")
response = s3_client.list_buckets()
return [b["Name"] for b in response.get("Buckets", [])]
# This is inefficient if called frequently
for _ in range(100):
get_bucket_names_bad()
```
## 2. Choose Clients vs. Resources Wisely
`boto3` offers two interfaces:
* **Clients**: Low-level, 1:1 mapping to AWS API calls. Provides fine-grained control.
* **Resources**: Higher-level, object-oriented abstraction. Offers automatic pagination, attribute access, and simpler syntax for common operations.
### ✅ GOOD: Leverage Resources for Simplicity and Automation
Use resources when available and when their higher-level abstractions simplify your code (e.g., S3, EC2, DynamoDB).
```python
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_s3.service_resource import Bucket, S3ServiceResource
S3_RESOURCE: S3ServiceResource = boto3.resource("s3")
def delete_all_objects_in_bucket(bucket_name: str) -> None:
bucket: Bucket = S3_RESOURCE.Bucket(bucket_name)
# Resource handles pagination automatically for collection iteration
for obj in bucket.objects.all():
obj.delete()
print(f"Deleted all objects from {bucket_name}")
# Example usage:
# delete_all_objects_in_bucket("my-test-bucket")
```
### ❌ BAD: Over-complicating with Clients when Resources are Better
Manually handling tasks that resources automate.
```python
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_s3.client import S3Client
S3_CLIENT: S3Client = boto3.client("s3")
def delete_all_objects_in_bucket_bad(bucket_name: str) -> None:
# Requires manual pagination and object key extraction
paginator = S3_CLIENT.get_paginator("list_objects_v2")
for page in paginator.paginate(Bucket=bucket_name):
objects_to_delete = [{"Key": obj["Key"]} for obj in page.get("Contents", [])]
if objects_to_delete:
S3_CLIENT.delete_objects(Bucket=bucket_name, Delete={"Objects": objects_to_delete})
print(f"Deleted all objects from {bucket_name}")
```
## 3. Efficient Pagination
AWS APIs often return results in pages. Always use `boto3`'s built-in paginators or resource collections to iterate through all results.
### ✅ GOOD: Using Paginators or Resource Collections
```python
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_ec2.client import EC2Client
from mypy_boto3_ec2.service_resource import EC2ServiceResource, Instance
EC2_CLIENT: EC2Client = boto3.client("ec2")
EC2_RESOURCE: EC2ServiceResource = boto3.resource("ec2")
def list_all_instance_ids_client() -> list[str]:
paginator = EC2_CLIENT.get_paginator("describe_instances")
instance_ids = []
for page in paginator.paginate():
for reservation in page.get("Reservations", []):
for instance in reservation.get("Instances", []):
instance_ids.append(instance["InstanceId"])
return instance_ids
def list_all_instance_ids_resource() -> list[str]:
# Resource collections handle pagination automatically
instance_ids = [instance.id for instance in EC2_RESOURCE.instances.all()]
return instance_ids
# Example usage:
# print(list_all_instance_ids_client())
# print(list_all_instance_ids_resource())
```
### ❌ BAD: Manual Pagination Loops
This is error-prone and verbose.
```python
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_ec2.client import EC2Client
EC2_CLIENT: EC2Client = boto3.client("ec2")
def list_all_instance_ids_manual_bad() -> list[str]:
instance_ids = []
next_token = None
while True:
kwargs = {"NextToken": next_token} if next_token else {}
response = EC2_CLIENT.describe_instances(**kwargs)
for reservation in response.get("Reservations", []):
for instance in reservation.get("Instances", []):
instance_ids.append(instance["InstanceId"])
next_token = response.get("NextToken")
if not next_token:
break
return instance_ids
```
## 4. Batch Operations
Always prefer batch operations when available to minimize API calls and improve efficiency.
### ✅ GOOD: Using Batch APIs
```python
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_sqs.client import SQSClient
SQS_CLIENT: SQSClient = boto3.client("sqs")
def send_batch_messages(queue_url: str, messages: list[str]) -> None:
entries = [{"Id": str(i), "MessageBody": msg} for i, msg in enumerate(messages)]
# SQS allows up to 10 messages per batch
for i in range(0, len(entries), 10):
batch = entries[i:i+10]
SQS_CLIENT.send_message_batch(QueueUrl=queue_url, Entries=batch)
print(f"Sent {len(messages)} messages to {queue_url}")
# Example usage:
# send_batch_messages("https://sqs.us-east-1.amazonaws.com/123456789012/my-queue", ["msg1", "msg2", "msg3"])
```
### ❌ BAD: Looping Single Operations
```python
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_sqs.client import SQSClient
SQS_CLIENT: SQSClient = boto3.client("sqs")
def send_individual_messages_bad(queue_url: str, messages: list[str]) -> None:
for msg in messages:
# Each message sends a separate API call
SQS_CLIENT.send_message(QueueUrl=queue_url, MessageBody=msg)
print(f"Sent {len(messages)} messages to {queue_url}")
```
## 5. Type Hinting with `boto3-stubs`
`boto3`'s dynamic nature makes static analysis challenging. Use `boto3-stubs` to enable precise type checking, catch errors early, and improve IDE autocomplete.
### ✅ GOOD: Fully Type-Hinted Boto3 Code
Install `boto3-stubs` (e.g., `pip install 'boto3-stubs[s3]' mypy`).
```python
from typing import TYPE_CHECKING
import boto3
if TYPE_CHECKING:
from mypy_boto3_s3.client import S3Client
from mypy_boto3_s3.type_defs import BucketTypeDef, ListBucketsOutputTypeDef
S3_CLIENT: S3Client = boto3.client("s3")
def get_s3_bucket_names() -> list[str]:
response: ListBucketsOutputTypeDef = S3_CLIENT.list_buckets()
buckets: list[BucketTypeDef] = response.get("Buckets", [])
return [b["Name"] for b in buckets]
# Mypy will now correctly validate types and provide completions.
```
### ❌ BAD: Untyped Boto3 Code
This makes code harder to read, refactor, and debug.
```python
import boto3
s3_client = boto3.client("s3")
def get_s3_bucket_names_untyped():
response = s3_client.list_buckets()
# No type information here, relies on runtime inspection
return [b["Name"] for b in response.get("Buckets", [])]
```
## 6. Robust Error Handling
Boto3 operations can fail. Always wrap calls in `try...except` blocks and catch specific `botocore.exceptions` for graceful degradation.
### ✅ GOOD: Specific Exception Handling
```python
import boto3
from botocore.exceptions import ClientError
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_s3.client import S3Client
S3_CLIENT: S3Client = boto3.client("s3")
def download_s3_object(bucket_name: str, key: str, local_path: str) -> bool:
try:
S3_CLIENT.download_file(bucket_name, key, local_path)
print(f"Successfully downloaded {key} to {local_path}")
return True
except ClientError as e:
if e.response["Error"]["Code"] == "404":
print(f"Object {key} not found in bucket {bucket_name}.")
elif e.response["Error"]["Code"] == "403":
print(f"Permission denied to access {key} in bucket {bucket_name}.")
else:
print(f"An unexpected error occurred: {e}")
return False
except Exception as e:
print(f"A general error occurred: {e}")
return False
# Example usage:
# download_s3_object("my-test-bucket", "non-existent-file.txt", "/tmp/local.txt")
```
### ❌ BAD: Generic Exception Handling or No Handling
```python
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_s3.client import S3Client
S3_CLIENT: S3Client = boto3.client("s3")
def download_s3_object_bad(bucket_name: str, key: str, local_path: str) -> None:
try:
S3_CLIENT.download_file(bucket_name, key, local_path)
except Exception as e: # Too broad, hides specific issues
print(f"An error occurred: {e}")
# Or worse, no try/except at all.
```
## 7. Testing with `moto`
For unit and integration tests, `moto` provides a mock AWS environment, allowing you to test `boto3` interactions without hitting actual AWS services.
### ✅ GOOD: Using `moto` Decorators
```python
import boto3
import unittest
from moto import mock_aws
class TestS3Operations(unittest.TestCase):
@mock_aws
def test_create_and_list_bucket(self):
s3_client = boto3.client("s3", region_name="us-east-1")
bucket_name = "my-test-bucket"
# Create a bucket
s3_client.create_bucket(Bucket=bucket_name)
# List buckets and assert
response = s3_client.list_buckets()
self.assertIn(bucket_name, [b["Name"] for b in response["Buckets"]])
# Upload an object
s3_client.put_object(Bucket=bucket_name, Key="test.txt", Body="Hello Moto!")
# Download and verify
response = s3_client.get_object(Bucket=bucket_name, Key="test.txt")
self.assertEqual(response["Body"].read().decode("utf-8"), "Hello Moto!")
if __name__ == "__main__":
unittest.main()
```
### ❌ BAD: Mocking `boto3` Internals Manually
While `unittest.mock` is powerful, mocking `boto3` at a low level is tedious and fragile.
```python
import boto3
import unittest
from unittest.mock import patch
class TestS3OperationsManualMock(unittest.TestCase):
@patch("boto3.client")
def test_create_bucket_manual_mock(self, mock_boto_client):
# Configure the mock client to return specific values
mock_s3_client = mock_boto_client.return_value
mock_s3_client.list_buckets.return_value = {"Buckets": []}
mock_s3_client.create_bucket.return_value = {} # Simulate success
s3_client = boto3.client("s3") # This will now return our mock
s3_client.create_bucket(Bucket="my-test-bucket")
mock_s3_client.create_bucket.assert_called_once_with(Bucket="my-test-bucket")
# This approach quickly becomes complex for more involved interactions.
```