Daptal Ms
Daptal Ms

Reputation: 381

Node Js AWS lambda layers code to run locally

I have a code that runs a lambda function and uses lambda layers. The code works as expected in AWS environment.

For simple lambdas i can run the lambda locally by providing the prerequisite 'env' file and running using the command

node --input-type=module -e "import {handler} from './index.js'; console.log(await handler(JSON.parse(fs.readFileSync('./event.json'))));";

For layers that has only simple logic and no nodemodules i tried creating a directory /opt and copied the contents of the layer over and it works as expected without any code changes.

But when I tried to run the code with lambda layers that has nodemodule references, locally it throws the error module not found

Is there a way this can be done ? Any references will be appreciated.

Thanks

Upvotes: 0

Views: 253

Answers (1)

ofri cofri
ofri cofri

Reputation: 1026

Can you run your local tests using AWS SAM CLI? That's what AWS suggests, and what I did in the past, when facing a similar issue.

# Invoking function with event file
$ sam local invoke "Ratings" -e event.json

# Invoking function with event via stdin
$ echo '{"message": "Hey, are you there?" }' | sam local invoke --event - "Ratings"

# For more options
$ sam local invoke --help

Source: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-invoke.html

Upvotes: 0

Related Questions