aws / aws/aws-toolkit-vscode

Debug Sam CDK node typescript Lambda: "Bad handler"

Open
#4,504 0 comments 0 reactions 0 assignees View on GitHub
cdk guidance lambda language:node language:typescript sam
Dominant language
TypeScript
Stars
2k
Forks
807
Avg merge
10h 12m
Merged PRs (30d)
7

Description

## System details (run the `AWS: About Toolkit` command)

- OS: MacOs 14.3.1
- Visual Studio Code version: Version: 1.87.0
- AWS Toolkit version: v2.12.0

## Question

Hi, I have a test project with a lambda, based off: ( https://github.com/aws-samples/aws-saas-factory-ref-solution-serverless-saas ) I am able to invoke with sam local from the cli and it works perfectly, but vsCode debug fails, I am fairly sure it's a vsCode configuration issue.

note: I tried both `GetOrderLambdaHandle` and `getOrder` in the `lambdaHandler` field of the configuration, same error.

Thank You!

Details:
Cli Invoke ( Works ):

`sam local invoke GetOrderLambdaHandle -e ./test/events/basicEvent.json -t ./cdk.out/OrderServiceStack.template.json`

Basic Error:
Runtime.MalformedHandlerName","errorMessage":"Bad handler","stack":["Runtime.MalformedHandlerName: Bad handler"," at _splitHandlerString (file:///var/runtime/index.mjs:990:15)

Error and Supporting Details :
```
Debugger attached.
2024-03-05T15:10:26.087Z undefined ERROR Uncaught Exception {"errorType":"Runtime.MalformedHandlerName","errorMessage":"Bad handler","stack":["Runtime.MalformedHandlerName: Bad handler"," at _splitHandlerString (file:///var/runtime/index.mjs:990:15)"," at Object.load (file:///var/runtime/index.mjs:1118:36)"," at start (file:///var/runtime/index.mjs:1282:42)"," at file:///var/runtime/index.mjs:1288:7"," at ModuleJob.run (node:internal/modules/esm/module_job:218:25)"," at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)"," at async loadESM (node:internal/process/esm_loader:28:7)"," at async handleMainPromise (node:internal/modules/run_main:113:12)"]}
Waiting for the debugger to disconnect...
05 Mar 2024 15:10:26,145 [ERROR] (rapid) Init failed error=Runtime exited with error: exit status 129 InvokeID=
```

VS Code Config:

```
{
"type": "aws-sam",
"request": "direct-invoke",
"name": "Get Order",
"invokeTarget":{
"target": "code",
"architecture": "x86_64",
"projectRoot": "${workspaceFolder}/platform/aws-saas-factory-ref-solution-serverless-saas/server/OrderService",
"lambdaHandler": "getOrder",
},
"lambda": {
"runtime": "nodejs20.x",
"payload": {
"json": {
"requestContext": {
"authorizer": {
"tenantId": "2"
}
},
"pathParameters": {
"id": "2:234"
}
}
}
}
}
```

One Lambda:
```
import { Handler } from 'aws-cdk-lib/aws-lambda';
import { Logger } from "../common/logger";
import { OrderServiceDal } from "./order_service_dal"

const log = new Logger()
export const getOrder: Handler = async(event: any, context: any): Promise => {
console.log(event);
const tenantId = event['requestContext']['authorizer']['tenantId'];
log.logWithTenantContext(event, tenantId);
log.info("Get Order Request");

let params = event['pathParameters'];
let key = params['id'];
let order = await OrderServiceDal.get().getOrder(event, key);
return order;
}
```
Basic Stack:

```
import * as cdk from 'aws-cdk-lib';
import { Runtime, FunctionUrlAuthType } from "aws-cdk-lib/aws-lambda";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs"

import { Construct } from 'constructs';
import path = require('path');
// import * as sqs from 'aws-cdk-lib/aws-sqs';

export class OrderServiceStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const getOrderLambda = new NodejsFunction(this, "GetOrderLambdaHandle", {
runtime: Runtime.NODEJS_20_X,
entry: path.join(__dirname, `/business_layer/order_service.ts`),
handler: "getOrder"
});
}
}
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the direct-invoke configuration using the AWS SAM CLI command and the generated cdk.out/OrderServiceStack.template.json. Compare the VS Code launch fields with the CDK stack in the report, which uses server/OrderService/business_layer/order_service.ts and the getOrder handler. Done means identifying why VS Code debugging produces Bad handler while SAM invocation succeeds and documenting or correcting the affected launch behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, node.js, typescript, vscode
Domain
cloud, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.