We create tables in DynamoDB to store and organize data in the cloud. Tables help keep data safe and easy to find.
Table creation with AWS SDK in DynamoDB
const AWS = require('aws-sdk'); AWS.config.update({ region: 'us-east-1' }); const dynamodb = new AWS.DynamoDB(); const params = { TableName: 'YourTableName', KeySchema: [ { AttributeName: 'PrimaryKey', KeyType: 'HASH' } ], AttributeDefinitions: [ { AttributeName: 'PrimaryKey', AttributeType: 'S' } ], ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 } }; dynamodb.createTable(params, function(err, data) { if (err) console.log(err); else console.log('Table created:', data); });
KeySchema defines the primary key for the table.
ProvisionedThroughput sets how much read and write capacity the table has.
const params = {
TableName: 'Users',
KeySchema: [
{ AttributeName: 'UserId', KeyType: 'HASH' }
],
AttributeDefinitions: [
{ AttributeName: 'UserId', AttributeType: 'S' }
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
};const params = {
TableName: 'Orders',
KeySchema: [
{ AttributeName: 'OrderId', KeyType: 'HASH' },
{ AttributeName: 'OrderDate', KeyType: 'RANGE' }
],
AttributeDefinitions: [
{ AttributeName: 'OrderId', AttributeType: 'S' },
{ AttributeName: 'OrderDate', AttributeType: 'S' }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 5
}
};This program creates a DynamoDB table named 'Books' with 'ISBN' as the primary key. It sets read and write capacity to 5 units each.
const AWS = require('aws-sdk'); AWS.config.update({ region: 'us-east-1' }); const dynamodb = new AWS.DynamoDB(); const params = { TableName: 'Books', KeySchema: [ { AttributeName: 'ISBN', KeyType: 'HASH' } ], AttributeDefinitions: [ { AttributeName: 'ISBN', AttributeType: 'S' } ], ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 } }; dynamodb.createTable(params, function(err, data) { if (err) { console.log('Error:', err.message); } else { console.log('Table created:', data.TableDescription.TableName); } });
Make sure your AWS credentials and region are set correctly before running the code.
Table creation can take a few seconds; the callback confirms when it's ready.
Use meaningful table and key names to keep your data organized.
DynamoDB tables store data with a primary key to organize it.
Use AWS SDK to create tables by defining name, keys, and capacity.
Check the output to confirm the table was created successfully.
Practice
Solution
Step 1: Understand the role of a primary key in DynamoDB
The primary key uniquely identifies each item in the table, ensuring no duplicates.Step 2: Differentiate from other table settings
Read/write capacity, region, and encryption are important but unrelated to item uniqueness.Final Answer:
To uniquely identify each item in the table -> Option DQuick Check:
Primary key = unique item ID [OK]
- Confusing primary key with capacity settings
- Thinking primary key sets region or encryption
- Ignoring the uniqueness requirement
Solution
Step 1: Recall AWS SDK syntax for KeySchema
The correct key type for the partition key is "HASH" in the KeySchema array.Step 2: Identify incorrect key types
"PRIMARY", "PRIMARY_KEY", and "KEY" are not valid KeyType values in AWS SDK.Final Answer:
"KeySchema": [{ "AttributeName": "UserId", "KeyType": "HASH" }] -> Option AQuick Check:
KeyType for partition key = HASH [OK]
- Using invalid KeyType values like PRIMARY or KEY
- Confusing KeyType with attribute types
- Missing the array structure for KeySchema
const params = {
TableName: "Products",
KeySchema: [
{ AttributeName: "ProductId", KeyType: "HASH" }
],
AttributeDefinitions: [
{ AttributeName: "ProductId", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
};
const result = await dynamodb.createTable(params).promise();
console.log(result.TableDescription.TableName);Solution
Step 1: Understand the createTable response structure
On success, createTable returns an object with TableDescription including TableName.Step 2: Check the console.log statement
It prints result.TableDescription.TableName, which is "Products" as specified.Final Answer:
Products -> Option AQuick Check:
Successful createTable logs table name [OK]
- Expecting error messages on success
- Confusing undefined with valid output
- Missing await causing promise object logging
const params = {
TableName: "Orders",
KeySchema: [
{ AttributeName: "OrderId", KeyType: "HASH" },
{ AttributeName: "OrderDate", KeyType: "RANGE" }
],
AttributeDefinitions: [
{ AttributeName: "OrderId", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
};
await dynamodb.createTable(params).promise();Solution
Step 1: Check KeySchema and AttributeDefinitions consistency
Both "OrderId" and "OrderDate" are in KeySchema, but only "OrderId" is defined in AttributeDefinitions.Step 2: Identify missing attribute definition
"OrderDate" must be defined in AttributeDefinitions to avoid error.Final Answer:
Missing AttributeDefinition for "OrderDate" -> Option BQuick Check:
All key attributes need AttributeDefinitions [OK]
- Forgetting to define all key attributes
- Assuming RANGE is invalid KeyType
- Blaming ProvisionedThroughput or TableName
Solution
Step 1: Verify KeySchema order and types
Partition key must have KeyType "HASH" and sort key "RANGE" in correct order: EmployeeId (HASH), Department (RANGE).Step 2: Check AttributeDefinitions types
Both EmployeeId and Department are strings, so AttributeType "S" is correct for both.Step 3: Confirm ProvisionedThroughput values
ReadCapacityUnits: 3 and WriteCapacityUnits: 2 match the requirement.Final Answer:
{ TableName: "Employees", KeySchema: [{ AttributeName: "EmployeeId", KeyType: "HASH" }, { AttributeName: "Department", KeyType: "RANGE" }], AttributeDefinitions: [{ AttributeName: "EmployeeId", AttributeType: "S" }, { AttributeName: "Department", AttributeType: "S" }], ProvisionedThroughput: { ReadCapacityUnits: 3, WriteCapacityUnits: 2 } } -> Option CQuick Check:
Correct key order and attribute types = { TableName: "Employees", KeySchema: [{ AttributeName: "EmployeeId", KeyType: "HASH" }, { AttributeName: "Department", KeyType: "RANGE" }], AttributeDefinitions: [{ AttributeName: "EmployeeId", AttributeType: "S" }, { AttributeName: "Department", AttributeType: "S" }], ProvisionedThroughput: { ReadCapacityUnits: 3, WriteCapacityUnits: 2 } } [OK]
- Swapping HASH and RANGE key types
- Mismatching attribute types (string vs number)
- Omitting sort key in KeySchema
Ready to prove it?
Pass the test and this pattern moves unknown
