We use Boto3 to talk to AWS services like DynamoDB. The client and resource are two ways to do this. The client is low-level and direct, while the resource is easier and more Python-friendly.
Boto3 (Python) client vs resource in DynamoDB
import boto3 # Create client client = boto3.client('dynamodb') # Create resource resource = boto3.resource('dynamodb')
The client gives you direct access to DynamoDB API methods.
The resource provides higher-level, object-oriented access to DynamoDB.
client = boto3.client('dynamodb')
response = client.list_tables()resource = boto3.resource('dynamodb') table = resource.Table('MyTable') response = table.scan()
client.put_item(TableName='MyTable', Item={'Id': {'S': '123'}, 'Name': {'S': 'Alice'}})
table.put_item(Item={'Id': '123', 'Name': 'Alice'})This program lists DynamoDB tables using both client and resource methods and prints the results.
import boto3 # Using client client = boto3.client('dynamodb') tables_client = client.list_tables()['TableNames'] # Using resource resource = boto3.resource('dynamodb') tables_resource = [table.name for table in resource.tables.all()] print('Tables from client:', tables_client) print('Tables from resource:', tables_resource)
The client requires you to handle data types explicitly (e.g., {'S': 'string'} for strings).
The resource automatically converts Python types to DynamoDB types, making code cleaner.
Some advanced DynamoDB features might only be available via the client.
The client is low-level and matches AWS API calls exactly.
The resource is higher-level and easier to use with Python objects.
Choose client for full control and resource for simplicity.
Practice
client and resource when working with DynamoDB?Solution
Step 1: Understand the roles of client and resource
Theclientprovides direct access to AWS service APIs with low-level calls, matching the exact API structure.Step 2: Compare with resource abstraction
Theresourceoffers a higher-level, object-oriented interface that simplifies interactions with AWS services like DynamoDB.Final Answer:
clientis low-level and matches AWS API calls exactly, whileresourceis higher-level and easier to use. -> Option CQuick Check:
client = low-level, resource = high-level [OK]
- Thinking client is deprecated
- Confusing client and resource usage scope
- Assuming resource has full API coverage
Solution
Step 1: Recall Boto3 resource creation syntax
To create a resource, useboto3.resource('service_name'). For DynamoDB, it isboto3.resource('dynamodb').Step 2: Identify incorrect options
boto3.client('dynamodb')creates a client, not a resource.connectandsessionare not valid Boto3 methods for this purpose.Final Answer:
dynamodb = boto3.resource('dynamodb') -> Option BQuick Check:
Resource creation uses boto3.resource('service') [OK]
- Using boto3.client instead of boto3.resource
- Trying to use boto3.connect which doesn't exist
- Confusing session with resource creation
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Users')
response = table.get_item(Key={'UserId': '123'})
print(response['Item']['Name'])
What will this code print if the item exists with Name 'Alice'?Solution
Step 1: Understand the get_item call on resource Table
Theget_itemmethod returns a dictionary with an 'Item' key containing the requested item if it exists.Step 2: Access the 'Name' attribute from the returned item
The code printsresponse['Item']['Name'], which will be 'Alice' if the item exists with that attribute.Final Answer:
Alice -> Option DQuick Check:
response['Item']['Name'] = Alice [OK]
- Assuming response is just the item dict
- Forgetting to check 'Item' key
- Confusing client and resource response formats
import boto3
client = boto3.client('dynamodb')
response = client.get_item(TableName='Users', Key={'UserId': '123'})
print(response['Item'])Solution
Step 1: Check the Key format for client get_item
When using the low-level client, the Key must specify attribute types, e.g.,{'UserId': {'S': '123'}}.Step 2: Identify other issues
TableName is case-sensitive and usually capitalized as in the table creation. The methodget_itemexists on client. The print syntax is correct for a dict.Final Answer:
The Key value should be a dict with attribute type, e.g., {'UserId': {'S': '123'}} -> Option AQuick Check:
Client Key needs attribute type dict [OK]
- Using simple dict for Key with client
- Assuming client and resource Key formats are same
- Ignoring case sensitivity of TableName
Solution
Step 1: Understand resource update_item usage
Resource expects simple Python types for Key and ExpressionAttributeValues, so{'UserId': '123'}and{':val': 30}are correct.Step 2: Understand client update_item usage
Client requires attribute types in Key and ExpressionAttributeValues, so{'UserId': {'S': '123'}}and{':val': {'N': '30'}}are needed.Step 3: Confirm UpdateExpression syntax
UsingSET Age = :valis correct for updating an attribute value.Final Answer:
Resource and client update_item calls with correct Key and ExpressionAttributeValues formats as in option A -> Option AQuick Check:
Resource uses plain types, client uses typed dicts [OK]
- Mixing client and resource Key formats
- Using ADD instead of SET for update
- Passing raw values in client ExpressionAttributeValues
Ready to prove it?
Pass the test and this pattern moves unknown
