Create a DynamoDB Table in SST
We are now going to start creating our infrastructure in SST. Starting with DynamoDB.
Create a Table
Add the following to our infra/storage.ts
.
// Create the DynamoDB table
export const table = new sst.aws.Dynamo("Notes", {
fields: {
userId: "string",
noteId: "string",
},
primaryIndex: { hashKey: "userId", rangeKey: "noteId" },
});
Let’s go over what we are doing here.
We are using the Dynamo
component to create our DynamoDB table.
It has two fields:
userId
: The id of the user that the note belongs to.noteId
: The id of the note.
We are then creating an index for our table.
Each DynamoDB table has a primary key. This cannot be changed once set. The primary key uniquely identifies each item in the table, so that no two items can have the same key. DynamoDB supports two different kinds of primary keys:
- Partition key
- Partition key and sort key (composite)
We are going to use the composite primary key (referenced by primaryIndex
in code block above) which gives us additional flexibility when querying the data. For example, if you provide only the value for userId
, DynamoDB would retrieve all of the notes by that user. Or you could provide a value for userId
and a value for noteId
, to retrieve a particular note.
Deploy Changes
After you make your changes, SST will automatically create the table. You should see something like this at the end of the deploy process.
| Created Notes sst:aws:Dynamo
Now that our database has been created, let’s create an S3 bucket to handle file uploads.
For help and discussion
Comments on this chapter