Skip to main content

Overview

The DynamoDBTable construct creates an Amazon DynamoDB table with sensible defaults for serverless workloads. It supports on-demand and provisioned billing, global secondary indexes, streams, and point-in-time recovery.

Import

import { DynamoDBTable } from "@fjall/components-infrastructure/lib/resources/aws/database/dynamodb";

Basic Usage

const table = new DynamoDBTable(scope, "Sessions", {
  partitionKey: { name: "sessionId", type: "S" },
});

Using the Database Factory

The recommended way to add DynamoDB to a Fjall application is through the DatabaseFactory:
import { DatabaseFactory } from "@fjall/components-infrastructure/lib/patterns/aws/database";

const cache = app.addDatabase(
  DatabaseFactory.build("Cache", {
    type: "DynamoDB",
    partitionKey: { name: "id", type: "S" },
  }),
);

Properties

PropertyTypeDefaultDescription
partitionKey{ name: string, type: "S" | "N" | "B" }RequiredPartition key attribute
sortKey{ name: string, type: "S" | "N" | "B" }Sort key attribute
billingMode"PAY_PER_REQUEST" | "PROVISIONED""PAY_PER_REQUEST"Billing mode
readCapacitynumberRead capacity units (provisioned only)
writeCapacitynumberWrite capacity units (provisioned only)
globalSecondaryIndexesDynamoDBGlobalSecondaryIndex[]GSI definitions
timeToLiveAttributestringTTL attribute name
stream"NEW_IMAGE" | "OLD_IMAGE" | "NEW_AND_OLD_IMAGES" | "KEYS_ONLY"DynamoDB Streams mode
pointInTimeRecoverybooleantrueEnable point-in-time recovery
encryption"AWS_OWNED" | "AWS_MANAGED" | "CUSTOMER_MANAGED""AWS_OWNED"Encryption type
removalPolicy"DESTROY" | "RETAIN" | "SNAPSHOT""RETAIN"What happens on stack deletion

Key Types

Attribute types map to DynamoDB types:
TypeDynamoDB TypeUse For
"S"StringIDs, names, emails
"N"NumberTimestamps, counters
"B"BinaryHashes, compressed data

Methods

MethodReturnsDescription
getTableName()stringTable name
getTableArn()stringTable ARN
getTableStreamArn()string | undefinedStream ARN (if streams enabled)
getTable()ITableCDK Table construct
grantRead(grantee)GrantGrant read access
grantWrite(grantee)GrantGrant write access
grantReadWrite(grantee)GrantGrant read and write access
grantFullAccess(grantee)GrantGrant full access (including admin)
grantStreamRead(grantee)GrantGrant stream read access

Examples

Composite Key Table

const orders = app.addDatabase(
  DatabaseFactory.build("Orders", {
    type: "DynamoDB",
    partitionKey: { name: "customerId", type: "S" },
    sortKey: { name: "orderDate", type: "N" },
    pointInTimeRecovery: true,
  }),
);

Table with Global Secondary Index

const users = app.addDatabase(
  DatabaseFactory.build("Users", {
    type: "DynamoDB",
    partitionKey: { name: "userId", type: "S" },
    globalSecondaryIndexes: [
      {
        indexName: "emailIndex",
        partitionKey: { name: "email", type: "S" },
        projectionType: "ALL",
      },
    ],
  }),
);

Provisioned Throughput

const events = app.addDatabase(
  DatabaseFactory.build("Events", {
    type: "DynamoDB",
    partitionKey: { name: "pk", type: "S" },
    sortKey: { name: "sk", type: "S" },
    billingMode: "PROVISIONED",
    readCapacity: 100,
    writeCapacity: 50,
  }),
);

With DynamoDB Streams

const audit = app.addDatabase(
  DatabaseFactory.build("AuditLog", {
    type: "DynamoDB",
    partitionKey: { name: "entityId", type: "S" },
    sortKey: { name: "timestamp", type: "N" },
    stream: "NEW_AND_OLD_IMAGES",
  }),
);

// Connect a Lambda to process stream events
processor.addDynamoDbEventSource(audit);

TTL for Automatic Expiry

const sessions = app.addDatabase(
  DatabaseFactory.build("Sessions", {
    type: "DynamoDB",
    partitionKey: { name: "sessionId", type: "S" },
    timeToLiveAttribute: "expiresAt",
  }),
);

Connecting to Compute

Grant permissions to Lambda or ECS services:
// Read-only access
table.grantRead(readerLambda);

// Read-write access
table.grantReadWrite(apiService);

// Stream processing
table.grantStreamRead(processorLambda);

CloudFormation Outputs

Each DynamoDB table exports:
OutputDescription
{id}TableNameTable name
{id}TableArnTable ARN
{id}TableStreamArnStream ARN (if enabled)

Next Steps

Database Factory

Create databases via the factory pattern

Lambda Function

Process DynamoDB streams with Lambda