We are now going to start creating our infrastructure in SST using AWS CDK. Starting with DynamoDB.

Create a Stack

Change indicator Add the following to a new file in stacks/StorageStack.ts.

import { StackContext, Table } from "sst/constructs";

export function StorageStack({ stack }: StackContext) {
  // Create the DynamoDB table
  const table = new Table(stack, "Notes", {
    fields: {
      userId: "string",
      noteId: "string",
    },
    primaryIndex: { partitionKey: "userId", sortKey: "noteId" },
  });

  return {
    table,
  };
}

Let’s go over what we are doing here.

We are creating a new stack in our SST app. We will be using it to create all our storage related infrastructure (DynamoDB and S3). There’s no specific reason why we are creating a separate stack for these resources. It’s only meant as a way of organizing our resources and illustrating how to create separate stacks in our app.

We are using SST’s Table construct to create our DynamoDB table.

It has two fields:

  1. userId: The id of the user that the note belongs to.
  2. 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.

We are also returning the Table that’s being created publicly.

return {
  table,
};

Remove Template Files

The Hello World API that we previously created, can now be removed. We can also remove the files that came with the starter template.

Change indicator To remove the starter stack, run the following from your project root.

$ pnpm sst remove API

This will take a minute to run.

Change indicator Also remove the template files.

$ rm -r stacks/MyStack.ts packages/core/src/todo.ts packages/core/src/event.ts packages/functions/src/lambda.ts packages/functions/src/todo.ts packages/functions/src/events

Add to the App

Now let’s add our new stack to the app.

Change indicator Replace the sst.config.ts with this.

import { SSTConfig } from "sst";
import { StorageStack } from "./stacks/StorageStack";

export default {
  config(_input) {
    return {
      name: "notes",
      region: "us-east-1",
    };
  },
  stacks(app) {
    app.stack(StorageStack);
  },
} satisfies SSTConfig;

Deploy the App

If you switch over to your terminal, you’ll notice that you are being prompted to redeploy your changes. Go ahead and hit ENTER.

Note that, you’ll need to have sst dev running for this to happen. If you had previously stopped it, then running pnpm sst dev will deploy your changes again.

You should see something like this at the end of the deploy process.

✓  Deployed:
   StorageStack

Now that our database has been created, let’s create an S3 bucket to handle file uploads.