Skip to main content

Connection Options

dkmio is designed to be flexible with how it connects to AWS. The connection is lazy — the underlying boto3 resource is not created until the first operation is executed.

Option 1: Automatic

If no parameters are passed, dkmio will use the default credentials chain from your environment (e.g., AWS_DEFAULT_REGION, AWS_ACCESS_KEY_ID, IAM Roles).

from dkmio import DynamoDB

db = DynamoDB()

Option 2: Explicit Configuration

You can pass specific connection parameters directly to the DynamoDB constructor.

# Explicit region
db = DynamoDB(region_name="us-east-1")

# Local development (e.g., DynamoDB Local or Moto)
db = DynamoDB(
region_name="us-east-1",
endpoint_url="http://localhost:8000"
)

Option 3: Existing Boto3 Session

If you are already managing a boto3 Session (common in multi-account or multi-role environments), you can pass it directly.

import boto3

my_session = boto3.Session(profile_name="prod-profile")
db = DynamoDB(session=my_session)

Option 4: Existing Boto3 Resource

If your project already has a DynamoDB resource initialized, you can wrap it with dkmio.

import boto3

dynamodb_resource = boto3.resource("dynamodb")
db = DynamoDB(resource=dynamodb_resource)

Using the Default Database for Transactions

For standard table operations, the db instance is bound when you define your table class: class MyTable(db.Table).

However, for transactions, you might want to avoid passing the db instance every time. You can register a default database instance:

db = DynamoDB(region_name="us-east-1")
db.set_default() # Register as default for transactions

from dkmio import transaction

# Now you can omit db=db in transaction calls
with transaction.write() as tx:
tx.put(orders, ...)