Skip to content

local debug crashes for CDK stacks with props #24

@kristiandreher

Description

@kristiandreher

If you are trying to use local --debug for a CDK stack that relies on props it will crash due to the props being undefined.

Expected Behavior

It would be nice if samp local --debug would work for CDK stacks that relies on props.

Current Behavior

When starting samp local --debug on a CDK stack that relies on props it crashes.

VS Code Debug console

08.55.42 - Found 0 errors. Watching for file changes.
Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'tableName')
    at LambdaStack (<redacted>/code/test/samp-cdk-debug/.samp-out/lib/lambda-stack.js:11:41)
    at <anonymous> (<redacted>/tools/image/packages/samp-cli/lib/node_modules/samp-cli/src/commands/local/cdk-wrapper.js:25:15)
    at Module._compile (internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (internal/modules/cjs/loader:1308:10)
    at Module.load (internal/modules/cjs/loader:1117:32)
    at Module._load (internal/modules/cjs/loader:958:12)
    at executeUserEntryPoint (internal/modules/run_main:81:12)
    at <anonymous> (internal/main/run_main_module:23:47)

Possible Solution

Not sure. Maybe it is necessary to instantiate the entire App, not only the stack.
I think this is the line were the stack is instantiated with an empty object as props.
https://github.com/ljacobsson/samp-cli/blob/main/src/commands/local/cdk-wrapper.js#L25

Steps to Reproduce (for bugs)

  1. Create a CDK app with below setup
  2. cdk deploy --all
  3. Run samp local --debug and select the lib/lambda-stack.ts to debug
  4. Start debugging in VSCode. Check output in Debug console.

bin/samp-debug.ts

import * as cdk from "aws-cdk-lib";
import { TableStack } from "../lib/table-stack";
import { LambdaStack } from "../lib/lambda-stack";

const app = new cdk.App();
const { table } = new TableStack(app, "SampCdkDebugTableStack");
new LambdaStack(app, "SampCdkDebugLambdaStack", { table });

lib/table-stack.ts

import { Stack, StackProps } from "aws-cdk-lib";
import { AttributeType, BillingMode, Table } from "aws-cdk-lib/aws-dynamodb";
import { Construct } from "constructs";

export class TableStack extends Stack {
  public readonly table: Table;

  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    this.table = new Table(this, "TestTable", {
      partitionKey: {
        name: "PK",
        type: AttributeType.STRING,
      },
      billingMode: BillingMode.PAY_PER_REQUEST,
    });
  }
}

lib/lambda-stack.ts

import { Stack, StackProps } from "aws-cdk-lib";
import { ITable } from "aws-cdk-lib/aws-dynamodb";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Construct } from "constructs";

export type LambdaStackProps = StackProps & {
  table: ITable;
};

export class LambdaStack extends Stack {
  constructor(scope: Construct, id: string, props: LambdaStackProps) {
    super(scope, id, props);

    const fn = new NodejsFunction(this, "test", {
      environment: {
        TABLE_NAME: props.table.tableName,
      },
    });
    props.table.grantReadData(fn);
  }
}

Environment

  • SAM Patterns CLI version: 1.0.52
  • Node version: 18.15.0
  • NPM version: 9.5.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions