Overview
TheDynamoDBTable 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
Basic Usage
Using the Database Factory
The recommended way to add DynamoDB to a Fjall application is through theDatabaseFactory:
Properties
| Property | Type | Default | Description |
|---|---|---|---|
partitionKey | { name: string, type: "S" | "N" | "B" } | Required | Partition key attribute |
sortKey | { name: string, type: "S" | "N" | "B" } | — | Sort key attribute |
billingMode | "PAY_PER_REQUEST" | "PROVISIONED" | "PAY_PER_REQUEST" | Billing mode |
readCapacity | number | — | Read capacity units (provisioned only) |
writeCapacity | number | — | Write capacity units (provisioned only) |
globalSecondaryIndexes | DynamoDBGlobalSecondaryIndex[] | — | GSI definitions |
timeToLiveAttribute | string | — | TTL attribute name |
stream | "NEW_IMAGE" | "OLD_IMAGE" | "NEW_AND_OLD_IMAGES" | "KEYS_ONLY" | — | DynamoDB Streams mode |
pointInTimeRecovery | boolean | true | Enable 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:| Type | DynamoDB Type | Use For |
|---|---|---|
"S" | String | IDs, names, emails |
"N" | Number | Timestamps, counters |
"B" | Binary | Hashes, compressed data |
Methods
| Method | Returns | Description |
|---|---|---|
getTableName() | string | Table name |
getTableArn() | string | Table ARN |
getTableStreamArn() | string | undefined | Stream ARN (if streams enabled) |
getTable() | ITable | CDK Table construct |
grantRead(grantee) | Grant | Grant read access |
grantWrite(grantee) | Grant | Grant write access |
grantReadWrite(grantee) | Grant | Grant read and write access |
grantFullAccess(grantee) | Grant | Grant full access (including admin) |
grantStreamRead(grantee) | Grant | Grant stream read access |
Examples
Composite Key Table
Table with Global Secondary Index
Provisioned Throughput
With DynamoDB Streams
TTL for Automatic Expiry
Connecting to Compute
Grant permissions to Lambda or ECS services:CloudFormation Outputs
Each DynamoDB table exports:| Output | Description |
|---|---|
{id}TableName | Table name |
{id}TableArn | Table ARN |
{id}TableStreamArn | Stream ARN (if enabled) |
Next Steps
Database Factory
Create databases via the factory pattern
Lambda Function
Process DynamoDB streams with Lambda