diff --git a/.changeset/little-moles-begin.md b/.changeset/little-moles-begin.md new file mode 100644 index 0000000000..6c6f290511 --- /dev/null +++ b/.changeset/little-moles-begin.md @@ -0,0 +1,6 @@ +--- +"@serverless-stack/resources": patch +"@serverless-stack/docs": patch +--- + +DebugStack: allow customizing DynamoDB Table diff --git a/packages/resources/src/DebugStack.ts b/packages/resources/src/DebugStack.ts index 2879b1242b..be664926d6 100644 --- a/packages/resources/src/DebugStack.ts +++ b/packages/resources/src/DebugStack.ts @@ -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; + }; } /** @@ -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 diff --git a/packages/resources/test/DebugStack.test.ts b/packages/resources/test/DebugStack.test.ts new file mode 100644 index 0000000000..87afc54bb2 --- /dev/null +++ b/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" }], + }); +}); diff --git a/packages/resources/test/helper.ts b/packages/resources/test/helper.ts index 6845bb2d72..c70405a77f 100644 --- a/packages/resources/test/helper.ts +++ b/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 // diff --git a/www/docs/constructs/DebugStack.snippets.md b/www/docs/constructs/DebugStack.snippets.md index ad3b9d7480..80978a9013 100644 --- a/www/docs/constructs/DebugStack.snippets.md +++ b/www/docs/constructs/DebugStack.snippets.md @@ -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", });