Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading a packaged file in aws lambda package

Tags:

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 ?

like image 728
rOrlig Avatar asked Dec 09 '16 15:12

rOrlig


People also ask

What two application packaging formats does AWS Lambda support?

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.

Can we store files in Lambda?

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.


1 Answers

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.

like image 185
jaym Avatar answered Sep 18 '22 12:09

jaym