Skip to content

Commit f1a1d9d

Browse files
feat: creates dynamo table
1 parent de3b071 commit f1a1d9d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {Construct} from "constructs"
2+
import {RemovalPolicy} from "aws-cdk-lib"
3+
import {
4+
AttributeType,
5+
Billing,
6+
ProjectionType,
7+
TableEncryptionV2,
8+
TableV2
9+
} from "aws-cdk-lib/aws-dynamodb"
10+
import {Key} from "aws-cdk-lib/aws-kms"
11+
12+
export interface DynamoDbTableProps {
13+
readonly tableName: string
14+
readonly kmsKey: Key
15+
}
16+
17+
export class DynamoDbTable extends Construct {
18+
public readonly table: TableV2
19+
20+
constructor(scope: Construct, id: string, props: DynamoDbTableProps) {
21+
super(scope, id)
22+
23+
this.table = new TableV2(this, props.tableName, {
24+
tableName: props.tableName,
25+
partitionKey: {
26+
name: "pk",
27+
type: AttributeType.STRING
28+
},
29+
sortKey: {
30+
name: "sk",
31+
type: AttributeType.STRING
32+
},
33+
billing: Billing.onDemand(),
34+
encryption: TableEncryptionV2.customerManagedKey(props.kmsKey),
35+
removalPolicy: RemovalPolicy.DESTROY,
36+
pointInTimeRecoverySpecification: {
37+
pointInTimeRecoveryEnabled: true
38+
},
39+
// TODO: discuss TTL settings
40+
timeToLiveAttribute: "ttl"
41+
})
42+
43+
// GSI for reverse lookups if needed (session_id -> thread info)
44+
this.table.addGlobalSecondaryIndex({
45+
indexName: "session-index",
46+
partitionKey: {
47+
name: "session_id",
48+
type: AttributeType.STRING
49+
},
50+
projectionType: ProjectionType.ALL
51+
})
52+
}
53+
}

packages/cdk/resources/Storage.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Construct} from "constructs"
22
import {Key} from "aws-cdk-lib/aws-kms"
33
import {S3Bucket} from "../constructs/S3Bucket"
4+
import {DynamoDbTable} from "../constructs/DynamoDbTable"
45

56
export interface StorageProps {
67
readonly stackName: string
@@ -9,6 +10,8 @@ export interface StorageProps {
910
export class Storage extends Construct {
1011
public readonly kbDocsBucket: S3Bucket
1112
public readonly kbDocsKey: Key
13+
public readonly conversationTable: DynamoDbTable
14+
public readonly conversationKey: Key
1215

1316
constructor(scope: Construct, id: string, props: StorageProps) {
1417
super(scope, id)
@@ -25,5 +28,17 @@ export class Storage extends Construct {
2528
kmsKey: this.kbDocsKey,
2629
versioned: true
2730
})
31+
32+
// create KMS key for conversation table encryption
33+
this.conversationKey = new Key(this, "ConversationKey", {
34+
enableKeyRotation: true,
35+
description: "KMS key for encrypting conversation sessions"
36+
})
37+
38+
// create DynamoDB table for conversation sessions
39+
this.conversationTable = new DynamoDbTable(this, "ConversationTable", {
40+
tableName: `${props.stackName}-conversations`,
41+
kmsKey: this.conversationKey
42+
})
2843
}
2944
}

0 commit comments

Comments
 (0)