Skip to content

Commit

Permalink
DebugStack: allow customizing DynamoDB Table (#1814)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwang committed Jun 16, 2022
1 parent 01307a0 commit 33b89b6
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/little-moles-begin.md
@@ -0,0 +1,6 @@
---
"@serverless-stack/resources": patch
"@serverless-stack/docs": patch
---

DebugStack: allow customizing DynamoDB Table
7 changes: 7 additions & 0 deletions packages/resources/src/DebugStack.ts
Expand Up @@ -24,6 +24,12 @@ export interface DebugStackProps extends cdk.StackProps {
* Lambda function props for WebSocket request handlers.
*/
websocketHandlerRoleArn?: string;
cdk?: {
/**
* Override the settings of the internally created DynamoDB table
*/
table?: Omit<dynamodb.TableProps, "partitionKey" | "sortKey">;
};
}

/**
Expand Down Expand Up @@ -60,6 +66,7 @@ export class DebugStack extends cdk.Stack {
partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING },
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
removalPolicy: cdk.RemovalPolicy.DESTROY,
...props?.cdk?.table,
});

// Create S3 bucket for storing large payloads
Expand Down
57 changes: 57 additions & 0 deletions packages/resources/test/DebugStack.test.ts
@@ -0,0 +1,57 @@
import { test, expect } from "vitest";
/* eslint-disable @typescript-eslint/ban-ts-comment*/

import {
ABSENT,
countResources,
hasResource,
hasResourceTemplate,
} from "./helper";
import * as cdk from "aws-cdk-lib";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
import { DebugApp, DebugStack } from "../src";

const lambdaDefaultPolicy = {
Action: ["xray:PutTraceSegments", "xray:PutTelemetryRecords"],
Effect: "Allow",
Resource: "*",
};

function createApp() {
return new DebugApp({
name: "my-app",
stage: "dev",
region: "us-east-1",
})
}

/////////////////////////////
// Test constructor
/////////////////////////////

test("cdk.table undefined", async () => {
const stack = new DebugStack(createApp(), "stack");
hasResource(stack, "AWS::DynamoDB::Table", {
TableName: ABSENT,
PointInTimeRecoverySpecification: ABSENT,
KeySchema: [{ AttributeName: "pk", KeyType: "HASH" }],
});
});

test("cdk.table defined", async () => {
const stack = new DebugStack(createApp(), "stack", {
cdk: {
table: {
pointInTimeRecovery: true
}
}
});
hasResource(stack, "AWS::DynamoDB::Table", {
TableName: ABSENT,
PointInTimeRecoverySpecification: {
PointInTimeRecoveryEnabled: true,
},
KeySchema: [{ AttributeName: "pk", KeyType: "HASH" }],
});
});
2 changes: 1 addition & 1 deletion packages/resources/test/helper.ts
@@ -1,5 +1,5 @@
import { Match, Matcher, MatchResult, Template } from "aws-cdk-lib/assertions";
import { Stack } from "../src";
import { Stack } from "aws-cdk-lib";

///////////////////////
// Matcher functions //
Expand Down
27 changes: 26 additions & 1 deletion www/docs/constructs/DebugStack.snippets.md
Expand Up @@ -97,12 +97,37 @@ scope.logicalPrefixedName("MyResource"); // Returns "dev-my-sst-app-MyResource"

This invokes the [`logicalPrefixedName`](DebugApp.md#logicalprefixedname) method in `DebugApp` that the `DebugStack` is added to. This'll return `dev-my-sst-app-debug-stack`, where `dev` is the current stage and `my-sst-app` is the name of the app.

### Configuring the debug stack
### Customizing stack name

```js
export function debugApp(app) {
new DebugStack(app, "debug-stack", {
stackName: app.logicalPrefixedName("my-debug-stack"),
});
}
```

### Configuring the DynamoDB table

Configure the internally created CDK `Table` instance.

```js
export function debugApp(app) {
new DebugStack(app, "debug-stack", {
cdk: {
table: {
pointInTimeRecovery: true,
}
}
});
}
```

### Using existing resources

```js
export function debugApp(app) {
new DebugStack(app, "debug-stack", {
payloadBucketArn: "arn:aws:s3:::my-bucket",
websocketHandlerRoleArn: "arn:aws:iam::123456789012:role/my-role",
});
Expand Down

0 comments on commit 33b89b6

Please sign in to comment.