简体   繁体   中英

AWS lambda module not found error when debugging locally using SAM cli and AWS CDK?

I am trying to debug lambda function locally using SAM cli and AWS CDK. So I am getting error function module not found any idea why so? I have taken this project from github https://github.com/mavi888/cdk-serverless-get-started

function.js:

exports.handler = async function (event) {
  console.log("request:", JSON.stringify(event));

  // return response back to upstream caller
  return sendRes(200, "HELLLOOO");
};

const sendRes = (status, body) => {
  var response = {
    statusCode: status,
    headers: {
      "Content-Type": "text/html",
    },
    body: body,
  };
  return response;
};

Inside lib folder

// lambda function
    const dynamoLambda = new lambda.Function(this, "DynamoLambdaHandler", {
      runtime: lambda.Runtime.NODEJS_12_X,
      code: lambda.Code.asset("functions"),
      handler: "function.handler",
      environment: {
        HELLO_TABLE_NAME: table.tableName,
      },
    });

I am using cdk synth > template.yaml command which generates cloud formation template.yaml file. Now I find function name with logicalID eg: myFunction12345678 and then trying to debug it locally using this command sam local invoke myFunction12345678 in my case it is DynamoLambdaHandler function. I get function module not found error. Any idea what I am missing?

Code is available on github: https://github.com/mavi888/cdk-serverless-get-started

在此处输入图像描述

The issue is that sam runs a Docker container with a Volume mount from the current directory. So, it's not finding the Lambda code because the path to the code from your CloudFormation template that CDK creates does not include the cdk.out directory in which cdk creates the assets.

You have two options:

  1. Run your sam command with a defined volume mount sam local invoke -v cdk.out
  2. Run the command from within the cdk.out directory and pass the JSON template as an argument since cdk writes a JSON template: sam local invoke -t <StackNameTemplate.json>

I'd recommend the latter because you're working within the framework that CDK creates and not creating additional files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM