https://serverless-stack.com/ logo
#guide
Title
# guide
r

Robin

10/19/2021, 8:04 AM
Hi all - first time trying out SST (thought I've been using the guide for years) and I'm blown away! (Will effuse in the general channel later) Comment on the NextJS example and tutorial: Can I suggest that we include a removal policy for the DynamoDB table. I realise it introduces a couple of lines into the code and the need for a brief explanation. But, I feel that's better than leaving a new user with an orphaned Table in their account. Happy to raise a PR if that helps. Example:
Copy code
import * as sst from "@serverless-stack/resources";
import { RemovalPolicy } from "@aws-cdk/core";

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

    // Counter table - DynamoDB
    const table = new sst.Table(this, "Counter", {
      fields: {
        counter: sst.TableFieldType.STRING
      },
      primaryIndex: { partitionKey: "counter" },
      dynamodbTable: { removalPolicy: RemovalPolicy.DESTROY }
    });

    // Next.js app
    const site = new sst.NextjsSite(this, "Site", {
      path: "frontend",
      environment: {
        // Pass the table details
        REGION: scope.region,
        TABLE_NAME: table.tableName
      }
    });

    // NextJS access perms
    site.attachPermissions([table]);

    // Display site URL
    this.addOutputs({
      URL: site.url
    });
  }
}
t

Tomasz Sobczyk

10/19/2021, 12:34 PM
I agree that those things should be covered - at least mentioned that you can do that - otherwise newbies have no idea what's going on 😄
f

Frank

10/19/2021, 3:45 PM
@Robin @Tomasz Sobczyk awesome suggestion!
@Jay we should mention
setDefaultRemovalPolicy
in all the SST example, or set it by default. What do u think?
r

Robin

10/19/2021, 3:57 PM
f

Frank

10/19/2021, 4:23 PM
Yeah that’s the one ^^ 👍
t

Tomasz Sobczyk

10/19/2021, 4:25 PM
I would suggest to set it up by default to destroy stuff and then mention that this won't fly for prod :)
j

Jay

10/19/2021, 5:56 PM
Yeah this is a good idea. I think the example should have that setting but only for your local stages. So people don’t leave it on by mistake?
t

Tomasz Sobczyk

10/19/2021, 9:48 PM
I mean... I'm hoping no one goes to prod without any thinking right after reading the guide.
r

Robin

10/20/2021, 1:06 PM
@Jay can you set removal policies per stage?
t

Tomasz Sobczyk

10/20/2021, 1:41 PM
To keep it sunstainable - I have various env files for different stages - put there control parameters - and then do some variable statements within my stacks, e.g.:
And sometimes a small helper if needed
if I need to map to to an enum like in this case or like this one:
I will only auto-delete objects if there is such an explicit flag in the config - to avoid a situattion where we forgot about it or sth when creating a new stage
j

Jay

10/20/2021, 6:56 PM
That’s a cool pattern @Tomasz Sobczyk. Thanks for sharing!
j

Joe Kendal

10/20/2021, 7:13 PM
By default it’s best to not delete databases, you never know until you know
Would be better to have an SST construct that automatically migrated the old data to the new table before deleting the old table? That way prod can be protected against downtime.
What is your strategy for handling this issue in prod? If you have to delete a table without disrupting users?
t

Tomasz Sobczyk

10/20/2021, 8:05 PM
In prod fully agree - nothing that is stateful should be allowed to be deleted
Still, when you are deploy ing yourself to prod you need to really stop for a moment and think about disasters, recovery, backup, etc. There are no good enough defaults here - you need to think about what is important for you
j

Joe Kendal

10/20/2021, 8:08 PM
I guess the solution is to just design the right partition key from the start so that we don’t ever run into a situation that requires replacing the table. I guess this just means having a suitable release cycle as opposed to instantly promoting every commit to prod.
Thankfully I have not moved to live Prod yet so haven’t run into this problem
But I am worried for some reason. Cloudformation can throw surprises sometimes where the only solution seems to be fatal
Idempotent replays with event bridge seems like the best solution
f

Frank

10/21/2021, 4:51 PM
@Robin
can you set removal policies per stage?
Yup, you set the removal policy if
this.stage !== "prod"
, where
this
is the
<http://sst.App|sst.App>
object if you are doing this inside
index
6 Views