Introduction
Are you looking to efficiently insert data into AWS DynamoDB? In this comprehensive blog post, we will guide you step-by-step on how to integrate AWS Lambda and Boto3 library to streamline data insertion processes.
Discover essential tips and best practices for optimizing database management and leveraging cloud services for peak performance. Get ready to unlock the power of AWS DynamoDB and take your data management to the next level. So lets get started!
NOTE: We will be using the ap-south-1 region for this tutorial, but you can use any region. Just make sure you create the lambda function and Dynamodb table in the same region.
Table of Contents
Open Table of Contents
What we will create?
To understand more clearly we will create a Todo list-taking application. For creating a to-do task we will save the id, name, and description of the task. We will be using “id” as the partition key, “name” as sort key while configuring the schema.
Creating lambda function
To create a lambda function
- Open the lambda service.
- Click on the Create function button.
- Provide a name for the lambda function.
- Select the runtime for the function.
- Select the architecture.
- Select a role to provide permission to the function.
- Leave the advanced settings as it is.
- Click on “Create function” button.
Permissions to the lambda function
The role which you are attaching to the lambda function must contain the following policy. This policy allows the lambda function to perform certain operations on the DynamoDB table.
- AmazonDynamoDBFullAccess
Creating a DynamoDB table
To create a DynamoDB table
- Open the DynamoDB service.
- From the available on the left click on “Tables”.
- Then click on the “Create Table” button on the right.
- Provide a table name.
- Enter the partition key as “id” with datatype as number.
- Enter the sort key as “name” with datatype as string.
- Select the customized settings option.
- Leave the table class as the default.
- Select the on-demand option for Read/write capacity settings.
- Leave the rest of the settings as it is.
- Click on the “Create Table” button.
Writing Lambda code
The following lambda code inserts an item in the specified DynamoDB table and return success message if the operation is successful.
import json
import boto3
# INPUT
DB_REGION = 'ap-south-1'
DB_TABLE_NAME = 'todo-app-db'
ITEM = {
'id': 1, # number
'name': 'My First todo', # string
'description': 'This is a sample item' # string
}
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb', region_name=DB_REGION)
table = dynamodb.Table(DB_TABLE_NAME)
response = table.put_item(Item=ITEM)
if response['ResponseMetadata']['HTTPStatusCode']==200:
return {"message":"Item inserted successfully!"}
else:
return {"message":"Operation Failes!"}
Key points to keep in mind when working with Dynamodb
- You can’t skip the partition key when defining the DynamoDB table schema. Your partition key must contain unique values.
- Always try to configure a sort key in the schema. Sort key helps to improve the data indexing which directly improves your data search in the table.
- Turn on the deletion protect when creating a DynamoDB table.
Conclusion
In conclusion, we have explored the seamless process of inserting data into AWS DynamoDB using AWS Lambda and Boto3. By following the step-by-step guide and leveraging best practices, you can efficiently manage your database and optimize performance.
The integration of AWS Lambda and Boto3 allows for streamlined data insertion processes, eliminating the need for manual operations and enhancing scalability. With the power of serverless computing and AWS cloud services, you can take full control of your data management, ensuring seamless operations and unlocking new possibilities for your applications. Keep experimenting and exploring the vast capabilities of AWS DynamoDB to harness the true potential of your data.
Resource
Follow the complete series to learn how to perform CRUD operations on AWS DynamoDB using AWS Lambda - Click Here