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:
- We create a DynamoDB table.
- We create a Lambda function.
- We grant the Lambda permission to write to the table (IAM).
- 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.

Step 3: Select Dynamodb and press on create table

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

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.

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

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

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

Step 10: Press on create function.

Step 11: Give name and Runtime.

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

Step 13: Now go to the code section and add the below code.
#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:

Troubleshooting Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| AccessDeniedException | Missing IAM Permissions. | Go to your Lambda's Configuration > Permissions tab. Ensure the Execution Role has AmazonDynamoDBFullAccess attached. |
| ResourceNotFoundException | Wrong Table Name. | Check line dynamodb.Table('UserTable'). Does it match your DynamoDB table name exactly (case-sensitive)? |
| ValidationException | Missing Key. | Your put_item call must include the Partition Key (UserId) defined when you created the table. |

