Create a DynamoDB Table

The creation of a DynamoDB table is the first step in utilizing this NoSQL database service. You begin by defining the primary key that uniquely identifies each item in the table and setting up the initial configuration for how the data is managed. Here’s how you can create a table using AWS SDKs for Node.js and Python:

Node.js:

const AWS = require('aws-sdk');  
AWS.config.update({region: 'eu-west-2'});  
const dynamodb = new AWS.DynamoDB();  
      
const params = {  
    TableName: 'UserTable',  
    KeySchema: [{AttributeName: 'UserID', KeyType: 'HASH'}], // Partition key  
    AttributeDefinitions: [{AttributeName: 'UserID', AttributeType: 'S'}],  
    ProvisionedThroughput: {ReadCapacityUnits: 1, WriteCapacityUnits: 1}  
};  
      
dynamodb.createTable(params, function(err, data) {  
    if (err) console.error('Error', err);  
    else  console.log('Table Created', data);  
});

This Node.js script uses the AWS SDK to configure the region, define a new DynamoDB table with a primary key, set the provisioned throughput, and create the table, outputting the result to the console.

Python:

import boto3  
      
dynamodb = boto3.resource('dynamodb', region_name='eu-west-2')  
      
table = dynamodb.create_table(  
    TableName='UserTable',  
    KeySchema=[{'AttributeName': 'UserID', 'KeyType': 'HASH'}], # Partition key  
    AttributeDefinitions=[{'AttributeName': 'UserID', 'AttributeType': 'S'}],  
    ProvisionedThroughput={'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1}  
)  
      
print("Table status:", table.table_status)

The Python script utilizes Boto3 to create a DynamoDB resource, defines a new table with a primary key and provisioned throughput, and then creates the table, printing the table status.