2

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

enter image description here

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
rock stone
  • 473
  • 2
  • 9
  • 20

1 Answers1

4

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.

cynicaljoy
  • 2,047
  • 1
  • 18
  • 25
  • But why does `sam local invoke myLanbdaFunction` is not working? I was referring this https://tlakomy.com/run-cdk-lambda-function-locally I am geenrating template.yaml file and then invoking the function that should work right? – rock stone Nov 15 '20 at 18:16
  • There are a few reasons: (1) `myFunction` is not the name of the resource in the CloudFormation template (2) for the reason that I provided in my answer -- it should not work. CDK and SAM are two separate projects that are maintained by separate teams within AWS. The fact that you _can_ make them work together does not mean that it should be expected. – cynicaljoy Nov 16 '20 at 00:34
  • I'm still getting the same error while using the second option. `"errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'chrome-aws-lambda'..."` – fillipvt Mar 06 '21 at 22:51