Inserting Data into AWS DynamoDB using AWS Lambda and Boto3

Efficient Data Insertion into AWS DynamoDB with AWS Lambda and Boto3: The Ultimate Guide


3 min read

Inserting Data into AWS DynamoDB using AWS Lambda and Boto3


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.

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

  1. Open the lambda service.

  2. Click on the Create function button.

  3. Provide a name for the lambda function.

  4. Select the runtime for the function.

  5. Select the architecture.

  6. Select a role to provide permission to the function.

  7. Leave the advanced settings as it is.

  8. 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

  1. Open the DynamoDB service.

  2. From the available on the left click on "Tables".

  3. Then click on the "Create Table" button on the right.

  4. Provide a table name.

  5. Enter the partition key as "id" with datatype as number.

  6. Enter the sort key as "name" with datatype as string.

  7. Select the customized settings option.

  8. Leave the table class as the default.

  9. Select the on-demand option for Read/write capacity settings.

  10. Leave the rest of the settings as it is.

  11. 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

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!"}
        return {"message":"Operation Failes!"}

Key points to keep in mind when working with Dynamodb

  1. You can't skip the partition key when defining the DynamoDB table schema. Your partition key must contain unique values.

  2. 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.

  3. Turn on the deletion protect when creating a DynamoDB table.


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.


Follow the complete series to learn how to perform CRUD operations on AWS DynamoDB using AWS Lambda - Click Here