Introduction
In the dynamic realm of cloud computing, efficient management of databases is quintessential. Enter DynamoDB, Amazon's fully managed NoSQL database service designed for seamless and scalable data storage. While DynamoDB offers remarkable capabilities, interaction with it demands a robust programming interface. This is where Boto3, the official AWS SDK for Python, steps in.
In this blog, we embark on a journey to explore DynamoDB CRUD operations using Boto3, unraveling the potential of these tools for efficient data management in the AWS cloud.
Please refer to my detailed blog on Mastering Amazon DynamoDB.
Demystifying Boto3: your AWS SDK Companion
Boto3 makes it easy to integrate your Python application, library, or script with AWS services including Amazon S3, Amazon EC2, Amazon DynamoDB, and more.
Consider boto3 your trusty bridge between your Python code and the vast AWS ecosystem. It provides pre-built functions and classes to interact with various AWS services, including DynamoDB. There is no need to reinvent the wheel, boto3 handles the low-level communication, allowing you to focus on your application logic.
Please refer References section below to get more details on Boto3 documentation.
Crafting CRUD Operations in DDB Table
Firstly, we need to create a Lambda function from the AWS Lambda Console which should be incorporated with the proper permissions to process the CRUD operations on the DynamoDB table. We should have DynamoDB Full Access permission for this and this is just for the demo, but in real-time, we need to follow the principle of least privilege (minimum permission required to work along within the functionality).
Please refer to my Mastering Serverless with AWS Lambda blog to check more on AWS Lambda and how we can create AWS Lambda Function from scratch and various other offerings that AWS Lambda provides.
I have written the code for performing CRUD operations on the DynamoDB table which can be found below based on the events on which we are invoking the lambda function, like Create event will create a new item in the table, Read event will read a specific item from the table, List event will read all the data from the table, Update event will modify the existing data, and it can add new data as well and at last we have Delete event that will delete the item from the table based on the primary key.
Let's start by creating a lambda function, replacing the existing boilerplate code with the updated code first which can be found below as per the CRUD functionality, which performs respective actions based on the events in the DynamoDB table, and then attaching the necessary IAM Permission to it as seen in the below screenshot.
import json
import boto3
dynamodb = boto3.resource('dynamodb')
table_name = 'ContactBookApp' # update the table name here.
table = dynamodb.Table(table_name)
def lambda_handler(event, context):
operation = event['operation']
if operation == 'create':
return create_item(event)
elif operation == 'read':
return read_item(event)
elif operation == 'list':
return list_items(event)
elif operation == 'update':
return update_item(event)
elif operation == 'delete':
return delete_item(event)
else:
return {
'statusCode': 400,
'body': json.dumps('Invalid operation')
}
def create_item(event):
item_data = event['item']
# everything is a string until you parse it.
# Ensure the 'id' field is cast to an integer
item_data['id'] = int(item_data['id'])
# we need to use put_item() boto3 api to create item in DDB Table
response = table.put_item(Item=item_data)
return {
'statusCode': 200,
'body': json.dumps(response)
}
def read_item(event):
item_id = int(event['item_id']) # Convert 'item_id' to an integer
# we need to use get_item() boto3 api to read item from DDB Table
response = table.get_item(Key={'id': item_id})
item = response.get('Item')
return {
'statusCode': 200,
'body': json.dumps(str(item)) # Convert 'item_id' to string
}
def list_items(event):
# we need to use scan() boto3 api to list all items in DDB Table
response = table.scan()
items = response.get('Items', [])
return {
'statusCode': 200,
'body': json.dumps(str(items)) # Convert 'item_id' to string
}
def update_item(event):
item_id = int(event['item_id']) # Convert 'item_id' to an integer
updated_data = event['updated_data']
# Prepare the update expression and attribute values dynamically
update_expression = 'SET '
expression_attribute_values = {}
for key, value in updated_data.items():
update_expression += f'#{key} = :{key}, '
expression_attribute_values[f':{key}'] = value
# Remove the trailing comma and space
update_expression = update_expression[:-2]
# we need to use update_item() boto3 api to update item in DDB Table
response = table.update_item(
Key={'id': item_id},
UpdateExpression=update_expression,
ExpressionAttributeNames={f'#{key}': key for key in updated_data.keys()},
ExpressionAttributeValues=expression_attribute_values
)
return {
'statusCode': 200,
'body': json.dumps(response)
}
def delete_item(event):
item_id = int(event['item_id']) # Convert 'item_id' to an integer
response = table.delete_item(Key={'id': item_id})
return {
'statusCode': 200,
'body': json.dumps(response)
}
Below is the DynamoDB table which does not have any items for now and as soon as we invoke the lambda function based on the particular events as per CRUD, the item will get created, read, listed, updated, and deleted to/from the DynamoDB table.
Events created as per DDB CRUD Operations
DynamoDB (DDB) which is a NoSQL database, aligns with standard CRUD operations - Create, Read, Update, and Delete and each operation is tailored to address distinct data manipulation requirements. Let's delve into each of the following events one by one.
Create Event (Adding data to DynamoDB)
The Create operation involves adding new items or records to a DynamoDB table. Using Boto3, this is achieved by providing the necessary attributes for the new item as a dictionary and calling the put_item
method.
{
"operation": "create",
"item": {
"id": 7777,
"name": "Ronnie",
"email": "ronnie@gmail.com",
"contactNo": "87090993",
"designation": "Manager",
"project": "CVB",
"newHire": "Yes"
}
}
We will get the following response after invoking the lambda function with the create event.
Now we can see the item created in the DynamoDB table.
Read Event (Retrieving data from DynamoDB)
The Read operation retrieves data from DynamoDB based on specified criteria. Boto3 offers the get_item
method for this purpose.
{
"operation": "read",
"item_id": 7777
}
We will get the following response after invoking the lambda function with the read event.
List Event (Retrieving all data from DynamoDB)
The List operation lists/retrieves all the data from DynamoDB. Boto3 offers the scan
method for this purpose, and we don't need to provide extra parameters in this case.
{
"operation": "list"
}
We will get the following response after invoking the lambda function with the list event.
Update Event (Modifying existing data or add new data into DynamoDB table)
The Update operation is used to modify existing data in DDB table items or add new data. Boto3 offers the update_item
method for this purpose.
{
"operation": "update",
"item_id": 7777,
"updated_data": {
"designation": "Senior Engg Manager",
"project": "QWE",
"awards": "yes"
}
}
This is the existing table item created as per the create event and now we are going to modify this item as per the update event as seen above.
We will get the following response after invoking the lambda function with the update event.
Now we can see that the item has been updated in the DynamoDB table as well.
Delete Event (Removing data from DynamoDB table)
The Delete operation involves removing items from the DynamoDB table. Boto3 provides the delete_item
method for this purpose.
{
"operation": "delete",
"item_id": 7777
}
We will get the following response after invoking the lambda function with the delete event.
Now we can see that the item has been deleted from the DynamoDB table.
Understanding these CRUD operations equips you with the foundational skills to manipulate data within DynamoDB using Boto3, offering a powerful toolset for managing your database in the AWS cloud.
References
Getting Started with Lambda Function
Conclusion
This blog furnishes comprehensive instructions on programmatically executing CRUD operations within a DynamoDB table. You have the flexibility to tailor these procedures to align with your specific table structures and desired functionality.
Thank you for dedicating time to read my blog! ๐ I trust you discovered it to be beneficial and insightful. If so, please show your appreciation with a ๐ like and consider ๐ subscribing to my newsletter for more content like this.
I'm continuously seeking ways to enhance my blog, so your comments or suggestions are always welcome ๐ฌ
Once again, thank you for your ongoing support!
Connect with me -
#aws #awscommunity #cloudcomputing #cloud