How to Use DynamoDB with Lambda: Simple Integration Guide
To use
DynamoDB with Lambda, create a Lambda function with permissions to access DynamoDB, then use the AWS SDK inside the function to read or write data. This lets your Lambda respond to events and interact with DynamoDB tables easily.Syntax
Here is the basic syntax to use DynamoDB in a Lambda function using the AWS SDK for JavaScript (v3):
importthe DynamoDB client and commands.- Create a DynamoDB client instance.
- Use commands like
PutItemCommandorGetItemCommandto write or read data. - Call
client.send(command)to execute the operation.
javascript
import { DynamoDBClient, PutItemCommand, GetItemCommand } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({ region: "us-east-1" }); // Example: Put item const putParams = { TableName: "YourTableName", Item: { "PrimaryKey": { S: "key1" }, "Attribute": { S: "value" } } }; const putCommand = new PutItemCommand(putParams); await client.send(putCommand); // Example: Get item const getParams = { TableName: "YourTableName", Key: { "PrimaryKey": { S: "key1" } } }; const getCommand = new GetItemCommand(getParams); const data = await client.send(getCommand); console.log(data.Item);
Example
This example shows a Lambda function that writes a new item to DynamoDB and then reads it back. It uses async/await and the AWS SDK v3.
javascript
import { DynamoDBClient, PutItemCommand, GetItemCommand } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({ region: "us-east-1" }); export const handler = async (event) => { const tableName = "MySampleTable"; const itemKey = "item123"; // Write item const putParams = { TableName: tableName, Item: { "Id": { S: itemKey }, "Message": { S: "Hello from Lambda!" } } }; await client.send(new PutItemCommand(putParams)); // Read item const getParams = { TableName: tableName, Key: { "Id": { S: itemKey } } }; const data = await client.send(new GetItemCommand(getParams)); return { statusCode: 200, body: JSON.stringify({ item: data.Item }) }; };
Output
{"statusCode":200,"body":"{\"item\":{\"Id\":{\"S\":\"item123\"},\"Message\":{\"S\":\"Hello from Lambda!\"}}}"}
Common Pitfalls
Common mistakes when using DynamoDB with Lambda include:
- Not granting the Lambda function the correct IAM permissions to access DynamoDB.
- Using the wrong region or table name in the client configuration.
- Incorrectly formatting the item attributes (DynamoDB expects types like
Sfor string,Nfor number). - Not handling async calls properly, causing the function to exit before DynamoDB operations complete.
javascript
/* Wrong: Missing IAM permission or wrong region */ const clientWrong = new DynamoDBClient({ region: "wrong-region" }); /* Right: Correct region and ensure IAM role has DynamoDB access */ const clientRight = new DynamoDBClient({ region: "us-east-1" });
Quick Reference
| Step | Description |
|---|---|
| 1. Create Lambda | Set up a Lambda function in AWS console or CLI. |
| 2. Add IAM Role | Attach a role with DynamoDB access permissions. |
| 3. Install SDK | Use AWS SDK v3 in your Lambda code. |
| 4. Initialize Client | Create DynamoDBClient with correct region. |
| 5. Use Commands | Use PutItemCommand, GetItemCommand, etc., to interact. |
| 6. Handle Async | Use async/await to wait for operations to finish. |
Key Takeaways
Grant your Lambda function IAM permissions to access DynamoDB tables.
Use AWS SDK v3 with DynamoDBClient and command objects inside Lambda.
Always specify the correct AWS region and table name in your code.
Format DynamoDB items with attribute types like S (string) and N (number).
Use async/await to ensure DynamoDB operations complete before Lambda exits.