What you'll learn
partitionKeyandsortKeyare attribute objects with a name and a type:STRING,NUMBERorBINARY.- The default billing mode is provisioned, 5 read and 5 write capacity units. Choose
PAY_PER_REQUESTdeliberately. - Point-in-time recovery is off by default. In current CDK it is set with
pointInTimeRecoverySpecification; the olderpointInTimeRecoveryboolean is deprecated.
Section 01
Keys and billing
The key schema can't be changed after the table exists, so the first decision is the most important. The Pokédex uses the Pokédex number as the partition key and a form id as the sort key, so one number can hold several forms (a regional variant, a mega evolution). The number is a Number: as a string, “10” would sort before “9”.
billingMode chooses between on-demand and provisioned capacity. On-demand (PAY_PER_REQUEST) is the simplest choice for a new, unpredictable workload.
import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
const table = new dynamodb.Table(this, "PokedexTable", {
partitionKey: { name: "pokedexNumber", type: dynamodb.AttributeType.NUMBER },
sortKey: { name: "formId", type: dynamodb.AttributeType.STRING },
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
pointInTimeRecoverySpecification: { pointInTimeRecoveryEnabled: true },
});Section 02
A global secondary index
To list Pokémon by type you add a global secondary index (GSI) with primaryType as its partition key. Call a method on the table, rather than passing a prop: that is the L2 style for things you can add later.
table.addGlobalSecondaryIndex({
indexName: "byType",
partitionKey: { name: "primaryType", type: dynamodb.AttributeType.STRING },
});Section 03
Read the template
Open the output of the exercise: the table becomes AWS::DynamoDB::Table with KeySchema, AttributeDefinitions (only keys and index keys are listed there, because DynamoDB is schemaless for everything else), BillingMode and GlobalSecondaryIndexes. With on-demand billing, CDK also omits provisioned throughput from the index.
Like buckets, tables default to RETAIN. That is the right default for data you care about. Production keeps it; Quest 9 shows how to choose per environment.
Practice
Your turn
Describe the Pokédex table. Its partition key is the Pokédex number (a Number) and its sort key is the form (a String) — the starter has the wrong key type and no sort key. Bill on demand, turn on point-in-time recovery, and add a global secondary index named byType on the primaryType String attribute.
pokedexNumber NUMBER + formId STRING keys, PAY_PER_REQUEST, point-in-time recovery, and a byType index.The simulator runs the constructs this course teaches, plus simple loops and helper methods. Other CDK code may not be recognised.
Check
Check your understanding
Quick Quiz · one question, as many tries as you need.
Choose one answer.