Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requiring file from layer in a NodeJS lambda

I am trying to import a config.json file from a lambda layer into a lambda running on NodeJS 8.10. I have found some tutorials online but couldn't make it import the file. As for the official documention, there's little to no indication on how to proceed.

I have a Lambda called LayerTester with the following index.js file:

const a = require('/opt/config/config.json');

exports.handler = event => {

    console.log(a);

    return true;

};

I created a new layer named config. I prepared a .zip file with a single file called config.json.

er I tried zipping the folder and also zipping the file itself.

Then I linked the layer to the lambda:

enter image description here

I get the following error message:

Response:
{
  "errorMessage": "Cannot find module '/opt/config/config.json'",
  "errorType": "Error",
  "stackTrace": [
    "Function.Module._load (module.js:474:25)",
    "Module.require (module.js:596:17)",
    "require (internal/module.js:11:18)",
    "Object.<anonymous> (/var/task/index.js:1:73)",
    "Module._compile (module.js:652:30)",
    "Object.Module._extensions..js (module.js:663:10)",
    "Module.load (module.js:565:32)",
    "tryModuleLoad (module.js:505:12)",
    "Function.Module._load (module.js:497:3)"
  ]
}

Request ID:
"2295bde4-fbaf-4880-89f4-a5b5eeca4a8b"

Function Logs:
START RequestId: 2295bde4-fbaf-4880-89f4-a5b5eeca4a8b Version: $LATEST
Unable to import module 'index': Error
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/var/task/index.js:1:73)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
END RequestId: 2295bde4-fbaf-4880-89f4-a5b5eeca4a8b

I tried with different paths without any success. How can I import my config file successfully?

like image 714
Ivan Avatar asked Oct 26 '25 06:10

Ivan


1 Answers

if your config.json is in the root of your zip file, you should import it as /opt/config.json. the layer name is not part of layer's extracted path

like image 159
Reza Nasiri Avatar answered Oct 28 '25 21:10

Reza Nasiri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!