How to Use AWS SDK for DynamoDB: Simple Guide
To use
aws-sdk for DynamoDB, first install the SDK and create a DynamoDB client. Then, use client methods like putItem or getItem to interact with your DynamoDB tables by passing parameters in the correct format.Syntax
The AWS SDK for DynamoDB requires creating a client object and calling its methods with parameters. Key parts include:
- Importing the SDK: Load the AWS SDK module.
- Creating a DynamoDB client: Initialize with region and credentials.
- Calling methods: Use methods like
putItem,getItem,updateItem, etc., with parameters specifying table name and item data.
javascript
const { DynamoDBClient, PutItemCommand } = require("@aws-sdk/client-dynamodb"); const client = new DynamoDBClient({ region: "us-east-1" }); const params = { TableName: "YourTableName", Item: { "PrimaryKey": { S: "KeyValue" }, "Attribute": { S: "Value" } } }; const command = new PutItemCommand(params); client.send(command).then(data => console.log(data)).catch(err => console.error(err));
Example
This example shows how to add an item to a DynamoDB table named Users using the AWS SDK for JavaScript v3. It creates a client, prepares the item data, sends the PutItemCommand, and logs the success or error.
javascript
import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb"; async function addUser() { const client = new DynamoDBClient({ region: "us-east-1" }); const params = { TableName: "Users", Item: { "UserId": { S: "123" }, "Name": { S: "Alice" }, "Age": { N: "30" } } }; try { const command = new PutItemCommand(params); const data = await client.send(command); console.log("Item added successfully", data); } catch (err) { console.error("Error adding item", err); } } addUser();
Output
Item added successfully {}
Common Pitfalls
Common mistakes when using AWS SDK for DynamoDB include:
- Not formatting item attributes correctly (each attribute must specify its type like
Sfor string,Nfor number). - Forgetting to await the
sendmethod or not handling promises properly. - Using wrong region or missing credentials causing authorization errors.
- Confusing
DocumentClient(from older SDK) with the v3 client which requires explicit attribute types.
javascript
/* Wrong: Missing attribute types */ const wrongParams = { TableName: "Users", Item: { "UserId": "123", // Missing { S: "123" } "Name": "Alice" } }; /* Right: Correct attribute types */ const rightParams = { TableName: "Users", Item: { "UserId": { S: "123" }, "Name": { S: "Alice" } } };
Quick Reference
Here is a quick reference for common DynamoDB attribute types used in AWS SDK v3:
| Attribute Type | Description | Example |
|---|---|---|
| S | String | {"Name": {"S": "Alice"}} |
| N | Number (stored as string) | {"Age": {"N": "30"}} |
| BOOL | Boolean | {"IsActive": {"BOOL": true}} |
| NULL | Null value | {"Deleted": {"NULL": true}} |
| M | Map (nested object) | {"Address": {"M": {"City": {"S": "NY"}}}} |
| L | List (array) | {"Tags": {"L": [{"S": "tag1"}, {"S": "tag2"}]}} |
Key Takeaways
Always create a DynamoDB client with correct region and credentials before calling commands.
Use AWS SDK v3 commands like PutItemCommand with properly typed attribute values.
Handle promises with async/await or .then/.catch to manage asynchronous calls.
Format item attributes with explicit types like S for string and N for number.
Check your AWS region and credentials to avoid authorization errors.