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

Debug Sam CDK node typescript Lambda: "Bad handler" #4504

Open
rognorak opened this issue Mar 5, 2024 · 0 comments
Open

Debug Sam CDK node typescript Lambda: "Bad handler" #4504

rognorak opened this issue Mar 5, 2024 · 0 comments
Labels
cdk guidance General information and guidance, answers to FAQs, or recommended best practices/resources. language:node debugging NodeJS / javascript / typescript lambdas using SAM local sam service:lambda

Comments

@rognorak
Copy link

rognorak commented Mar 5, 2024

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<any> => {
    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"
    });
  }
}
@rognorak rognorak added the guidance General information and guidance, answers to FAQs, or recommended best practices/resources. label Mar 5, 2024
@justinmk3 justinmk3 added language:node debugging NodeJS / javascript / typescript lambdas using SAM local service:lambda sam cdk labels Mar 5, 2024
@justinmk3 justinmk3 changed the title Debug Sam CDK Lambda with Node / VsCode Configuration Issue - Debug Sam CDK node Lambda: "Bad handler" Mar 5, 2024
@justinmk3 justinmk3 changed the title Debug Sam CDK node Lambda: "Bad handler" Debug Sam CDK node typescript Lambda: "Bad handler" Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cdk guidance General information and guidance, answers to FAQs, or recommended best practices/resources. language:node debugging NodeJS / javascript / typescript lambdas using SAM local sam service:lambda
Projects
None yet
Development

No branches or pull requests

2 participants