AWS DynamoDB - Insert Data Using AWS Lambda

Last Updated : 9 Jun, 2026

AWS Lambda can be integrated with DynamoDB to build scalable serverless applications. In this setup, Lambda executes Python code, IAM manages permissions, and DynamoDB stores the application data. Its architecture components are:

  • Lambda: Executes the Python code
  • IAM Role: Grants permissions to access DynamoDB
  • DynamoDB: Stores the data items

Implementation

Step 1: Sign in to your AWS account at the AWS Management Console.

Step 2: Create a DynamoDB Table

  • Search for DynamoDB in the AWS services search bar and open it.
  • Click Create table
  • Enter Table name and Partition key
  • Optionally add a sort key if required.
  • Click Create table
Screenshot-2026-06-04-095936

Note: The partition key you define here must be included in every put_item call from your Lambda function.

Step 3: Create an IAM Role for Lambda

  • Search for IAM in the AWS console
  • Navigate to Access Management → Roles
  • Click Create role
Screenshot-2026-06-04-100232

Step 4: Configure Trusted Entity

  • Select AWS service as the trusted entity type.
  • Select Lambda as the use case.
  • Click Next.
Screenshot-2026-06-04-100723
  • Attach Permissions
  • Search for and attach "AmazonDynamoDBFullAccess"
  • Click Next
Screenshot-2026-06-04-100858
  • Name the Role
  • Enter a role name
  • Click Create role
Screenshot-2026-06-04-101053

Note: For production systems, follow the Principle of Least Privilege and grant only the specific DynamoDB actions your function requires (e.g., dynamodb:PutItem) instead of full access.

Step 5: Create the Lambda Function

  • Navigate to AWS Lambda.
  • Click Create function.
  • Select Author from scratch.
  • Enter Function name
  • Choose Runtime Python 3.x
Screenshot-2026-06-04-101623

Step 6: Configure Execution Role

  • Expand Additional settings while creating the Lambda function.
  • Open Custom execution role.
  • Select Use an existing role.
  • Choose the IAM role created earlier.
  • Click on Save
  • Click Create function.
Screenshot-2026-06-04-102516

Step 7: Add Python Code

  • Open the Code tab of the Lambda function.
  • Replace the default Lambda handler code with the following:
Python
import json
import boto3

def lambda_handler(event, context):

    dynamodb = boto3.resource('dynamodb')

    # Replace 'sample' with your DynamoDB table name
    table = dynamodb.Table('sample')

    response = table.put_item(
        Item={
            'empid': 101,
            'name': 'Samar'
        }
    )

    return response
Screenshot-2026-06-04-104057

Step 8: Deploy and Test the Function

  • Click Deploy to save the code.
  • Open the Test tab.
  • Create a new test event.
  • Click Test to invoke the Lambda function.
Screenshot-2026-06-04-104333

Step 9: Verify Data in DynamoDB Table

  • Open DynamoDB from the AWS Console.
  • Select your table from the list.
  • Navigate to the Explore table items tab.
Screenshot-2026-06-04-104606
  • Click Scan or Run to view the table data.
Screenshot-2026-06-04-104930

Troubleshooting Common Errors

Error

CauseSolution

AccessDeniedException

Missing IAM permissions.Add "AmazonDynamoDBFullAccess" (or "dynamodb:PutItem") to Lambda's execution role under Configuration > Permissions.

ResourceNotFoundException

Wrong table name.Check "dynamodb.Table('name')". Match the DynamoDB table name exactly (case-sensitive).

ValidationException

Missing partition key.Ensure your "put_item" call includes the exact partition key defined during table creation.
Comment