How to create a DynamoDB table with the AWS CDK
With code examples in TypeScript, Python, Java, and C#
1: TypeScript
import * as dynamodb from '@aws-cdk/aws-dynamodb';
const table = new dynamodb.Table(this, 'my-table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING }
});
2: Python
import aws_cdk.aws_dynamodb as dynamodb
table = dynamodb.Table(self, "my-table",
partition_key=dynamodb.Attribute(
name="id",
type=dynamodb.AttributeType.STRING
)
)
3: Java
import software.amazon.awscdk.services.dynamodb.*;
Table table = Table.Builder.create(this, "my-table")
.partitionKey(
Attribute.builder().name("id").type(AttributeType.STRING).build())
.build();
4: CSharp
using Amazon.CDK.AWS.DynamoDB;
Table table = new Table(this, "my-table", new TableProps {
PartitionKey = new Attribute {
Name = "id",
Type = AttributeType.STRING
}
});
Want to learn how to use familiar programming languages to define your infrastructure as code? Check out the AWS Cloud Development Kit (CDK).
You can find many more examples in C#, Java, Python, TypeScript, and also Go at the AWS CDK examples GitHub repository.