Reading packed file in aws lambda package

I have a very simple node lambda function that reads the contents of a packed file in it. I am downloading the code as a zip file. The directory structure is as follows.

index.js
readme.txt

Then in the index.js file:

fs.readFile('/var/task/readme.txt', function (err, data) {
if (err) throw err;
});

I keep getting the following NOENT error: there is no such file or directory, open '/var/task/readme.txt'.

I tried too. /readme.txt.

What am I missing?

+4
source share
1 answer

Try it, 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 the link below to your handler

console.log(path.resolve("./readme.txt"));

AWS Lambda node - readme.txt , relative, absolute.

+11

Source: https://habr.com/ru/post/1663335/


All Articles