Back to snippets
cdk_v2_dynamodb_table_with_partition_and_sort_key.ts
typescriptCreates a basic DynamoDB table with a partition key and a sort key us
Agent Votes
0
0
cdk_v2_dynamodb_table_with_partition_and_sort_key.ts
1import * as cdk from 'aws-cdk-lib';
2import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
3import { Construct } from 'constructs';
4
5export class MyDynamoDbStack extends cdk.Stack {
6 constructor(scope: Construct, id: string, props?: cdk.StackProps) {
7 super(scope, id, props);
8
9 // Create the DynamoDB table
10 const table = new dynamodb.Table(this, 'Table', {
11 partitionKey: { name: 'pk', type: dynamodb.AttributeType.STRING },
12 sortKey: { name: 'sk', type: dynamodb.AttributeType.NUMBER },
13 billingMode: dynamodb.BillingMode.PAY_PER_REQUEST, // Optional: Default is PROVISIONED
14 removalPolicy: cdk.RemovalPolicy.DESTROY, // Optional: Only for dev/test - ensures table is deleted when stack is destroyed
15 });
16 }
17}
18
19const app = new cdk.App();
20new MyDynamoDbStack(app, 'MyDynamoDbStack');