I have a very simple node lambda function which reads the contents of packaged file in it. I upload the code as zip file. The directory structure is as follows.
index.js readme.txt
Then have in my index.js file:
fs.readFile('/var/task/readme.txt', function (err, data) { if (err) throw err; });
I keep getting the following error NOENT: no such file or directory, open '/var/task/readme.txt'.
I tried ./readme.txt also.
What am I missing ?
Your AWS Lambda function's code consists of scripts or compiled programs and their dependencies. You use a deployment package to deploy your function code to Lambda. Lambda supports two types of deployment packages: container images and . zip file archives.
With increased AWS Lambda ephemeral storage, you get access to a secure, low-latency ephemeral file system up to 10 GB. You can continue to use up to 512 MB for free and are charged for the amount of storage you configure over the free limit for the duration of invokes.
Try this, it works for me:
'use strict' let fs = require("fs"); let path = require("path"); exports.handler = (event, context, callback) => { // To debug your problem console.log(path.resolve("./readme.txt")); // Solution is to use absolute path using `__dirname` fs.readFile(__dirname +'/readme.txt', function (err, data) { if (err) throw err; }); };
to debug why your code is not working, add below link in your handler
console.log(path.resolve("./readme.txt"));
On AWS Lambda node process might be running from some other folder and it looks for readme.txt
file from that folder as you have provided relative
path, solution is to use absolute
path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With