Open In App

AWS DynamoDB - Insert Data Using AWS Lambda

Last Updated : 18 Nov, 2025
Comments
Improve
Suggest changes
3 Likes
Like
Report

Connecting AWS Lambda (compute) to DynamoDB (database) is one of the most fundamental patterns in serverless architecture. It allows you to save user data, logs, or transaction records without managing a single server.

In this guide, we build a simple system where:

  1. We create a DynamoDB table.
  2. We create a Lambda function.
  3. We grant the Lambda permission to write to the table (IAM).
  4. We write Python code (boto3) to insert data dynamically.

The Architecture

  • Lambda: Executes your Python script.
  • IAM Role: The "security badge" that lets Lambda talk to the database.
  • DynamoDB: The NoSQL database storing the items.

Implementation:

Follow the below steps to insert data into the DynamoDB table using AWS lambda:

Step 1: Login into AWS console.

Step 2: Search for dynamodb.

Image
 

Step 3: Select Dynamodb and press on create table

Image
 

Step 4: Now give the table name and keys accordingly to your requirement 

Image
 

Now table will be created.

Step 5: Now we need to create Identity and Access Management(IAM) role for that go and search for IAM role.

Step 6: Click on role in access management and click on create role.

Image
 
Image
 

Step 7: Here we need to select AWS service and lambda.

Image
 

Step 8: Here we need to add permission, as we are using dynamo db we need to add AmazonDynamoDBFullAccess Permissions policies

Image
 

Step 9: Now give the role name and select create role

Image
 

Step 10: Press on create function.

Image
 

Step 11: Give name and Runtime.

Image
 

Step 12: Change the Execution role to Use an existing role and select your role.

Image
 

Step 13: Now go to the code section and add the below code.

Python
#importing packages
import json
import boto3
#function definition
def lambda_handler(event,context):
    dynamodb = boto3.resource('dynamodb')
    #table name
    table = dynamodb.Table('sample')
    #inserting values into table
    response = table.put_item(
       Item={
            'sample': 'bhagi',
           
        }
    )
    return response

Output:

Image

Troubleshooting Common Errors

Error MessageCauseSolution
AccessDeniedExceptionMissing IAM Permissions.Go to your Lambda's Configuration > Permissions tab. Ensure the Execution Role has AmazonDynamoDBFullAccess attached.
ResourceNotFoundExceptionWrong Table Name.Check line dynamodb.Table('UserTable'). Does it match your DynamoDB table name exactly (case-sensitive)?
ValidationExceptionMissing Key.Your put_item call must include the Partition Key (UserId) defined when you created the table.

Article Tags :

Explore