Reputation: 4861
I am trying to create a CDK package, where I want a SNS, a lambda and a DynamoDB. Now, the SNS and lambda being stateless can be generated using the CDK package.
So that would mean that a change in code, would require the whole package to be deployed again, and that would mean that the SNS is created again.
If I do that for DynamoDB, I think I will lose data that is stored in it, as it would be re-created.
Wanted to understand what is the standard way to create stateful resources with CDK.
Upvotes: 0
Views: 612
Reputation: 1642
In this case you want to use removalPolicy: RemovalPolicy.RETAIN
on your DynamoDb resource.
Let's say that your table name is mytable
. Then a CDK implementation in Javascript(adjust it to your favourite language) will probably look like this:
const { RemovalPolicy } = require("aws-cdk-lib");
const { AttributeType, Table } = require("aws-cdk-lib/aws-dynamodb");
const { Construct } = require("constructs");
class Database extends Construct {
constructor(scope, id) {
super(scope, id);
this.myTable = this.createMyTable();
}
createMyTable() {
const myTable = new Table(this, 'myTable', {
partitionKey: {
name: 'id',
type: AttributeType.STRING
},
tableName: 'myTable',
removalPolicy: RemovalPolicy.RETAIN, // <----- Make sure to retain your old table on destroy
});
return myTable;
};
}
module.exports = { Database };
Upvotes: 2
Reputation: 340
CloudFormation doesnt delete dynamodb tables by default. Altough its recommended to keep them in separate stack so you wont delete them by accident. See replace policy docs. You can also create stack IAM policy to disable deletion of dynamodb to be extra sure
Upvotes: 2