Hello team, I have an existing locally running Dyn...
# help
j
Hello team, I have an existing locally running DynamoDB table that I would like to destroy so I can start over. How do I destroy the table so I can rebuild with an altered stack?
k
If you're looking to delete a table that was created with a previous stack, you can just remove it through the AWS console. You can also specify a removal policy that will destroy the table along w/ the rest of the stack when it's removed
Copy code
import { RemovalPolicy } from "aws-cdk-lib";

new Table(stack, "Table", {
  fields: {
    userId: "string",
    noteId: "string",
  },
  primaryIndex: { partitionKey: "noteId", sortKey: "userId" },
  cdk: {
    table: {
      removalPolicy: RemovalPolicy.DESTROY,
    },
  },
});
j
Hey thanks Kevin! Does adding key-value pairs in the
fields
object define additional attributes? Or are these reserved only for hash and range keys?
I'm trying to build a table with a list of predefined attributes.
k
No prob! That's just for the hash and range keys. The rest of the attributes can be defined on the fly, no schema required.
j
Yea, that's what I suspected. I'm using an ORM within the application code which appears to want attributes predefined.
Any way to build attributes at table creation?
k
You know what, I'm not too sure. I'm not working with an ORM for my use case so not sure how that would fit in
j
Thanks for the reply
@Kevin Grimm I removed the DynamoDB table in the AWS console. But the SST Console is still showing the table with the original field names.
Did I miss a step to remove the table from the local environment?
Does the
cdk.table?
property remove the resource from local if it does not exist on remote?
I'm a bit confused there...
k
Could be that it's a reference to the original stack outputs? Usually when I need to delete a stack (and the table), I'll remove the table and then delete the remainder of the stack.
To confirm, are you trying to clear the stack produced from running
npx sst start
?
j
Yes. I created a StorageStack which contains
new sst.Table(...)
. I run
npx sst start
and the table has been created. I updated the field names in the
fields
prop. SST does not detect stack changes. I deleted the DynamoDB table from the AWS Console. In the SST Console, the original table appears with the old records. I expected the table in the SST Console to have been rebuilt showing me the new field names and having no records.
Note I did NOT delete any stacks in CloudFormation from the AWS console. Only the tables themselves.
k
So I would run
npx sst remove
to remove the stack. It sounds like you want to simply redeploy the stack with an altered table? In that case removing the stack and redeploying is likely the easiest option. I'm not sure if stack changes can be detected and deployed by SST. https://docs.serverless-stack.com/packages/cli#remove-stack
j
Boom I think that's it. I saw this yesterday but initially thought it would remove local files for some reason. I'll give it a shot.
That worked! Thanks a lot Kevin!
f
Thanks @Kevin Grimm!
@Jason S as a side note, try not to delete any resources manually in your AWS Console. In this case, if you removed the
new Table
line in ur SST app, and then deploy, the table would’ve been removed.
k
Thanks for adding onto this @Frank! Taking less trips to the AW console in the future 😄
j
Thanks @Frank I figured that to be the case, thanks for the advice 🙏