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

local debug crashes for CDK stacks with props #24

Open
kristiandreher opened this issue Aug 31, 2023 · 3 comments
Open

local debug crashes for CDK stacks with props #24

kristiandreher opened this issue Aug 31, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@kristiandreher
Copy link

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
@kristiandreher kristiandreher added the bug Something isn't working label Aug 31, 2023
@ljacobsson
Copy link
Owner

Thanks for the very thorough report! Been busy with real work, but will look at this asap

@ljacobsson
Copy link
Owner

Ultimately what the code in cdk-wrapper does is that it's trying to create a map between Lambda functions in your synthed template and their locations on your local file system

I think I need to rethink this a bit. I've been working on CDK support for Python a bit and I'm using a completely different strategy there where I compare the md5 hashes in the CDK asset-folders and the local filesystem. With Pythin that's trivial since there's no compilation/minification going on.

I wonder if there's a way to get sourcemaps into the asset folders and simply direct the incoming requests that way.

To be continued...

@ljacobsson
Copy link
Owner

I should add that I was completely unexperienced with CDK before I wrote this :-D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants