Create DynamoDB Table Locally
Table of Contents
- Introduction
- Create a DynamoDB Table with Node.js
- Add Data to a DynamoDB Table with Node.js
- Test Results by Scanning the Table
- Conclusion
Introduction
AWS DynamoDB is a NoSQL database service on the cloud. You can create an AWS account, navigate to the DynamoDB section in the AWS Console, and start creating tables and adding items. Unlike SQL databases where you need to set up and manage your own server, AWS manages the server infrastructure for DynamoDB — it is a fully serverless database that you access via HTTP requests.
But what about local development? Just as you can set up a SQL database locally, you can also run DynamoDB on your local machine. You can refer to this blog post for instructions on setting up DynamoDB locally.
This guide will show you how to create a DynamoDB table in your local environment, add data to it, and verify the results.
The following example code uses Node.js running on macOS with npm version 6.12.0 and Node version v12.13.0.
Create a DynamoDB Table with Node.js
The following code requires the aws-sdk npm package to interact with DynamoDB from Node.js.
var AWS = require("aws-sdk");
function createTable() {
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000",
});
var dynamodb = new AWS.DynamoDB();
var params = {
TableName: "MyFakeFriend",
KeySchema: [
{ AttributeName: "name", KeyType: "HASH" },
{ AttributeName: "age", KeyType: "RANGE" },
],
AttributeDefinitions: [
{ AttributeName: "name", AttributeType: "S" },
{ AttributeName: "age", AttributeType: "S" },
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10,
},
};
dynamodb.createTable(params, function (err, data) {
if (err) {
console.error(
"Unable to create table. Error JSON:",
JSON.stringify(err, null, 2),
);
} else {
console.log(
"Created table. Table description JSON:",
JSON.stringify(data, null, 2),
);
}
});
}
createTable();
Save the code above to a file (for example, app.js), navigate to the file's directory, and run the following command:
node app.js
If there are no errors, you should see the following message in the response:
Created table. Table description JSON: {
"TableDescription": {
"AttributeDefinitions": [
{
"AttributeName": "name",
"AttributeType": "S"
},
{
"AttributeName": "age",
"AttributeType": "S"
}
],
"TableName": "MyFakeFriend",
"KeySchema": [
{
"AttributeName": "name",
"KeyType": "HASH"
},
{
"AttributeName": "age",
"KeyType": "RANGE"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": "...",
"ProvisionedThroughput": {
"LastIncreaseDateTime": "1970-01-01T00:00:00.000Z",
"LastDecreaseDateTime": "1970-01-01T00:00:00.000Z",
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 10
},
"TableSizeBytes": 0,
"ItemCount": 0,
"TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/MyFakeFriend"
}
}
In this example, we created a DynamoDB table called MyFakeFriend with name as the partition key and age as the sort key.
One of the benefits of setting up DynamoDB locally is that you can prototype and test your schema before deploying to the actual AWS DynamoDB service in the cloud. This is especially important because once you create a table in AWS DynamoDB, you cannot change the primary key, partition key, sort key, or local secondary index. Testing locally first helps you get the schema right before committing to it in production.
Add Data to a DynamoDB Table with Node.js
After creating the table, the following Node.js code shows how to add random data to it.
Save the following code as addData.js and run node addData.js from the command line. This script will generate 20 items in the MyFakeFriend table. You can modify the maximum item count from 20 to any number you want — for example, tens of thousands of records — so you can perform testing on the local environment before moving to the AWS cloud.
var AWS = require("aws-sdk");
var faker = require("faker");
function getAge() {
return Math.floor(Math.random() * 120) + 1;
}
function addData() {
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000",
});
for (let index = 0; index < 20; index++) {
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: "MyFakeFriend",
Item: {
name: faker.name.findName(),
age: getAge().toString(),
phone: faker.phone.phoneNumber(),
},
};
docClient.put(params, function (err, data) {
if (err) {
console.error("Unable to add data", JSON.stringify(err, null, 2));
} else {
console.log("Able to add data", data);
}
});
}
}
addData();
Test Results by Scanning the Table
To verify the results, you can run the following AWS CLI command to scan and display all the data in the table:
aws dynamodb scan --table-name MyFakeFriend --endpoint-url http://localhost:8000
Conclusion
In this guide, we covered how to work with DynamoDB locally using Node.js:
- Create a table with a defined schema (partition key and sort key)
- Add data using the
DocumentClientwith fake data generated by thefakerlibrary - Verify results by scanning the table using the AWS CLI
Setting up DynamoDB locally is a great way to prototype, test, and validate your database schema before deploying to the AWS cloud. It saves time and helps you avoid costly mistakes with immutable table configurations.