Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set removal policy of dynamo db tables #381

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/database-dynamodb-single-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ constructs:

> :warning: Modifying a table local secondary indexes configuration requires table re-creation. If you modify this setting after the table has been populated with data, you'll need to transfer all data from old table to the new one. You however won't loose any data as all tables are configured to be left as is when removed from a CloudFormation template.

### Removal policy

For CI environments with setup/teardown it can be useful to have tables deleted when serverless is removed. This property gives you the ability to
opt-in to delete the table during update replacement and deletion of the cloudformation stack.

> :warning: This is a destructive operation and should not be used for persistent environments due to the risk of losing data.

```yaml
constructs:
myTable:
# ...
removalPolicy: delete
```

## Extensions

You can specify an `extensions` property on the `database/dynamodb-single-table` construct to extend the underlying CloudFormation resources. In the exemple below, the DynamoDB Table CloudFormation resource generated by the `myTable` dynamodb-single-table construct will be extended with the new `TableClass: STANDARD_INFREQUENT_ACCESS` CloudFormation property.
Expand Down
6 changes: 5 additions & 1 deletion src/constructs/aws/DatabaseDynamoDBSingleTable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Construct as CdkConstruct } from "constructs";
import type { CfnResource } from "aws-cdk-lib";
import { CfnOutput, Fn, Stack } from "aws-cdk-lib";
import { CfnOutput, Fn, RemovalPolicy, Stack } from "aws-cdk-lib";
import type { CfnTable } from "aws-cdk-lib/aws-dynamodb";
import { AttributeType, BillingMode, StreamViewType, Table } from "aws-cdk-lib/aws-dynamodb";
import type { FromSchema } from "json-schema-to-ts";
Expand All @@ -14,6 +14,7 @@ const DATABASE_DEFINITION = {
type: { const: "database/dynamodb-single-table" },
localSecondaryIndexes: { type: "boolean" },
gsiCount: { type: "integer", minimum: 1, maximum: 20 },
removalPolicy: { type: "string", enum: ["destroy", "retain"] },
},
additionalProperties: false,
} as const;
Expand All @@ -23,6 +24,7 @@ const DATABASE_DEFAULTS: Required<Configuration> = {
type: "database/dynamodb-single-table",
localSecondaryIndexes: false,
gsiCount: 0,
removalPolicy: "retain",
};

export class DatabaseDynamoDBSingleTable extends AwsConstruct {
Expand All @@ -44,6 +46,8 @@ export class DatabaseDynamoDBSingleTable extends AwsConstruct {
pointInTimeRecovery: true,
timeToLiveAttribute: "TimeToLive",
stream: StreamViewType.NEW_AND_OLD_IMAGES,
removalPolicy:
resolvedConfiguration.removalPolicy === "destroy" ? RemovalPolicy.DESTROY : RemovalPolicy.RETAIN,
});

if (resolvedConfiguration.localSecondaryIndexes) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/databasesDynamoDBSingleTable/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ constructs:
table:
Properties:
TableClass: STANDARD_INFREQUENT_ACCESS
databaseWithRemovalPolicyDestroy:
type: database/dynamodb-single-table
removalPolicy: destroy
7 changes: 7 additions & 0 deletions test/unit/databasesDynamoDBSingleTable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,11 @@ describe("databasesDynamoDBSingleTable", () => {
TableClass: "STANDARD_INFREQUENT_ACCESS",
});
});

it("allows setting table removal policy", () => {
expect(cfTemplate.Resources[computeLogicalId("databaseWithRemovalPolicyDestroy", "Table")]).toMatchObject({
UpdateReplacePolicy: "Delete",
DeletionPolicy: "Delete",
});
});
});
Loading