Skip to content

Table operations

Create, check, and delete DynamoDB tables programmatically.

Key features

  • Create tables with hash key and optional range key
  • On-demand or provisioned billing
  • Customer managed encryption (KMS)
  • Wait for table to become active
  • Check if table exists

Getting started

Create a table

Use create_table() to create a new DynamoDB table:

from pydynox import DynamoDBClient


def create_users_table():
    client = DynamoDBClient()

    # Simple table with hash key only
    client.create_table(
        "users",
        hash_key=("pk", "S"),
        wait=True,  # Wait for table to be ready
    )


def create_orders_table():
    client = DynamoDBClient()

    # Table with hash key and range key
    client.create_table(
        "orders",
        hash_key=("pk", "S"),
        range_key=("sk", "S"),
        wait=True,
    )

The hash_key and range_key are tuples of (attribute_name, attribute_type). Attribute types:

Type Description
"S" String
"N" Number
"B" Binary

Check if table exists

Before creating a table, check if it already exists:

client = DynamoDBClient()

if not client.table_exists("users"):
    client.create_table("users", hash_key=("pk", "S"), wait=True)

Delete a table

client = DynamoDBClient()
client.delete_table("users")

Warning

This permanently deletes the table and all its data. There is no confirmation prompt.

Advanced

Billing modes

DynamoDB offers two billing modes:

Mode Best for Cost
PAY_PER_REQUEST Unpredictable traffic Pay per read/write
PROVISIONED Steady traffic Fixed monthly cost

On-demand (PAY_PER_REQUEST) is the default. For provisioned capacity:

from pydynox import DynamoDBClient


def create_provisioned_table():
    client = DynamoDBClient()

    # Provisioned capacity (fixed cost, predictable performance)
    client.create_table(
        "high_traffic_table",
        hash_key=("pk", "S"),
        billing_mode="PROVISIONED",
        read_capacity=100,
        write_capacity=50,
        wait=True,
    )


def create_encrypted_table():
    client = DynamoDBClient()

    # Customer managed KMS encryption
    client.create_table(
        "sensitive_data",
        hash_key=("pk", "S"),
        encryption="CUSTOMER_MANAGED",
        kms_key_id="arn:aws:kms:us-east-1:123456789:key/abc-123",
        wait=True,
    )


def create_infrequent_access_table():
    client = DynamoDBClient()

    # Infrequent access class (cheaper storage, higher read cost)
    client.create_table(
        "archive",
        hash_key=("pk", "S"),
        table_class="STANDARD_INFREQUENT_ACCESS",
        wait=True,
    )

Table class

Choose a storage class based on access patterns:

Class Best for
STANDARD Frequently accessed data (default)
STANDARD_INFREQUENT_ACCESS Data accessed less than once per month

Infrequent access costs less for storage but more for reads.

Encryption

DynamoDB encrypts all data at rest. You can choose who manages the encryption key:

Option Description
AWS_OWNED AWS manages the key (default, free)
AWS_MANAGED AWS KMS manages the key (costs extra)
CUSTOMER_MANAGED You manage the key in KMS (full control)

For CUSTOMER_MANAGED, you must provide the KMS key ARN.

Wait for table

Tables take a few seconds to create. Use wait=True to block until the table is ready:

client.create_table("users", hash_key=("pk", "S"), wait=True)
# Table is now ready to use

Or wait separately:

client.create_table("users", hash_key=("pk", "S"))
# Do other setup...
client.wait_for_table_active("users", timeout_seconds=30)

Create table parameters

Parameter Type Default Description
table_name str Required Name of the table
hash_key tuple Required (name, type) for partition key
range_key tuple None (name, type) for sort key
billing_mode str "PAY_PER_REQUEST" Billing mode
read_capacity int 5 RCU (only for PROVISIONED)
write_capacity int 5 WCU (only for PROVISIONED)
table_class str "STANDARD" Storage class
encryption str "AWS_OWNED" Encryption type
kms_key_id str None KMS key ARN
wait bool False Wait for table to be active