Create a DynamoDB Table in SST

Link to chapter — https://serverless-stack.com/chapters/create-a-dynamodb-table-in-sst.html

In this step, we are defining a DynamoDB table in stacks/StorageStack.js as follows:

import * as sst from "@serverless-stack/resources";

export default class StorageStack extends sst.Stack {
  // Public reference to the table
  table;

  constructor(scope, id, props) {
    super(scope, id, props);

    // Create the DynamoDB table
    this.table = new sst.Table(this, "Notes", {
      fields: {
        userId: sst.TableFieldType.STRING,
        noteId: sst.TableFieldType.STRING,
      },
      primaryIndex: { partitionKey: "userId", sortKey: "noteId" },
    });
  }
}

When elaborating, it stages that:

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

// Public reference to the table
table;

This’ll allow us to reference this resource in our other stacks.

I’m not sure why we really need this, as this.table is already set in constructor as a public field.
Wouldn’t the following suffice?

export default class StorageStack extends sst.Stack {
      constructor(scope, id, props) {
            super(scope, id, props)

            // Create the DynamoDB table
            this.table = new sst.Table(this, 'Notes', {
                  fields: {
                        userId: sst.TableFieldType.STRING,
                        noteId: sst.TableFieldType.STRING
                  },
                  primaryIndex: {
                        partitionKey: 'userId',
                        sortKey: 'noteId'
                  }
            })
      }
}

So for example, you can still access the table when you create an object from the StorageClass:

const myStorageStack = new StorageStack() // Not sure what should be passed into the constructor yet
myStorageStack.table // The property is present

Hello,
I have two questions:

  1. In Remove Template Files section, is my-stack a placeholder?

  2. npx sst start had previously created two stacks. ([See the post I detailed about it]
    (Create an SST app - #2 by omertoraman))
    However, when I ran npx sts remove my-stack, I don’t see any of my stacks on the CloudFormation Console being removed.
    The output of the npx sts remove my-stack is as follows:

Warning: Setting the stage in the "sst.json" will be deprecated soon. Read more about this change here: https://docs.serverless-stack.com/working-locally#deprecating-the-stage-option-in-the-sstjson
Using stage: dev
Preparing your SST app
Transpiling source
Linting source
Removing dev-notes-my-stack

 ✅  dev-notes-my-stack


Stack dev-notes-my-stack
  Status: removed

Yeah it should be present. It’s more of a personal preference I guess. I like to explicitly list them up top so other people on my team know what it is exposing. Maybe we should clarify that.

That’s strange. If the terminal reports that the stack was removed, it should’ve been. Can you double check that it’s still around?

Hi and thanks for a great guide!

I have completed the guide and is trying to create multiple tables now. By looking at the dynamodb tab in aws management system, this was done correctly. However I am unable to put/post new data to this table.

I added this to the routes in ApiStack.js:

“POST /events”: “src/createEvent.main”,

Added this to StorageStack.js:

this.table2 = new sst.Table(this, “Events”, {

  fields: {

    eventId: sst.TableFieldType.STRING,

  },

  primaryIndex: { partitionKey: "eventId" },

});

with the public table2;

Copy pasted create.js to createEvent.js, but changing the following:

Item: {

  // The attributes of the item to be created

  eventId: uuid.v1(), // A unique uuid

  name: data.content, // Parsed from request body

  max_players: data.max_players,

  createdAt: Date.now(), // Current Unix timestamp

},

};

Finally I created the NewEvent.js frontend page which should be able to put data into the Events table:

function createEvent(content) {

return API.post("notes", "/events", {

    body: content

});

}

I get the error that: {error: ‘One or more parameter values were invalid: Missing the key userId in the item’}

So it is trying to write data to the other table Notes. How do I specify that I want to write data to the Events table from this page?

Thanks,
Nicklas

I think the Slack community would be the best place this!

https://serverless-stack.com/slack

Hello. I’m working on the guide. Up to the ‘Create a DynamoDB Table in SST’ section:

Getting an error:

Property 'TableFieldType' does not exist on type 'typeof import("path/to/repos/notes/node_modules/@serverless-stack/resources/dist/index")'.
11.       fields: {
12.         userId: sst.TableFieldType.STRING,
13.         noteId: sst.TableFieldType.STRING,
          }

I look more closely and it seems sst.TableFieldType is not attached to SST?

Can you check the version of SST you are using? Can I also see the entire file, and not just this snippet?

Thanks for responding @jayair

It seems I am on version “1.0.0-beta.5”

The code is the same as in the tutorial page:

import * as sst from "@serverless-stack/resources";

export default class StorageStack extends sst.Stack {
  // Public reference to the table
  table;

  constructor(scope, id, props) {
    super(scope, id, props);

    // Create the DynamoDB table
    this.table = new sst.Table(this, "Notes", {
      fields: {
        userId: sst.TableFieldType.STRING,
        noteId: sst.TableFieldType.STRING,
      },
      primaryIndex: { partitionKey: "userId", sortKey: "noteId" },
    });
  }
}

I tried starting the guide from scratch a few times again this evening, but still get the same error.

/home/b/repos/notes/stacks/StorageStack.js (14,21): Property 'TableFieldType' does not exist on type 'typeof import("/home/b/repos/notes/node_modules/@serverless-stack/resources/dist/index")'.
12.         userId: sst.TableFieldType.STRING,
13.         noteId: sst.TableFieldType.STRING,
14.       },

I noticed the TableFieldType isn’t in the module dist folder

I spoke to the team about this. It was an issue on our side. Running this:

npx create-serverless-stack@latest notes

Was installing the beta release. We fixed that but you’ll need to manually fix it for your project. Can you change the SST versions in your package.json to the latest stable release v0.69.3 instead?

Worked after updating my package.json:

"dependencies": {
    "@serverless-stack/cli": "0.69.3",
    "@serverless-stack/resources": "0.69.3",
    "aws-cdk-lib": "2.15.0"
  }

Thanks for your help!

1 Like

Thanks for the guides!
I am using typescript
export function StorageStack({ stack, app }: { stack: any; app: any }) { ... }
what are the types of stack and app please?

Also is it possible to create a composite key with 3 keys? if not, can I have another filter?

We’ll update the guide to TS soon but for now you can follow this instead: Learn SST | SST

1 Like