25

Can any one help me that why i got this issue I run this code locally it runs perfectly but at aws lambda i got this error even i increase the time over lambda time out function as well memory.

In this code i do a basic task for get call i just convert a xlsx to json and in post i just convert a test dir to zip file.I tried it from last few hrs for uploading at aws lambda now I am stuck and seeing continously this error can anyone help me out from this situation thanks in advance.

here is my code

index.js

"use strict"
const fs = require("fs");
const path = require("path");
const ctrlFuns = require("./functionality");
const output = fs.createWriteStream(path.join(__dirname, 
"./assets/docs.zip"));
const archiver = require("archiver");
const zipArchive = archiver("zip", {
gzip: true,
zlib: {
    level: 9
} // Sets the compression level.
});

exports.handleHttpRequest = function (event, context, callback) {

  if (event.http_method == "GET") {
    ctrlFuns.xlsxToJson().then((jsonObjs) => {
        callback(null, {
            users: jsonObjs,
        });
    }).catch((err) => {
        callback(err);
    });
} 
else if (event.http_method == "POST") {
    fs.readFile(path.join(__dirname + "/test/test.xlsx"), (err, file) => {
        if (err) {
            callback(err);
        } else {

            //pipe archive data to the file
            zipArchive.pipe(output);

            zipArchive.append(file, {
                name: "test.xlsx",
                prefix: "test-data" //used for folder name in zip file
            });

            // to catch this error explicitly
            zipArchive.on("error", (err) => {
                callback(err);
            });


            //to perform end tasks while zip converted
            zipArchive.on("end", () => {
                fs.readFile(path.join(__dirname + "/assets/docs.zip"), (err, 
    success) => {
                    if (err) {
                        callback(err);
                    } else {
                        callback(null, success.toString("base64"));
                    }
                });
            });
            //filnalizing the zip file for user use
            zipArchive.finalize();
        }
    });
} 
else {
    callback(null, "run default case");
}
} //handler-closes

here is my functionality.js

/**
 * OBJECTIVE: TO CREATE THE FUNCTINALITY
 */
"use strict"

const XLSX = require("xlsx");
const fs = require("fs");
const path = require("path");


var ctrlFuns = {};

ctrlFuns.xlsxToJson = function () {
 return new Promise((resolve, reject) => {
    fs.readFile(path.join(__dirname + "/test/test.xlsx"), (err, file) => {
        if (err) {
            reject(err);
        } else {
            let workbook = XLSX.read(file.buffer, {
                type: "buffer"
            });

            //if workbook is null
            if (!workbook) {
                reject("Workbook not found.");
            }

            /* Getting first workbook sheetName */
            let first_sheet_name = workbook.SheetNames[0];

            /* Get worksheet */
            let worksheet = workbook.Sheets[first_sheet_name];

            /**Convert Into JSON */
            resolve(XLSX.utils.sheet_to_json(worksheet, {
                raw: true
            }));
        }
    });
})

 } //fun-closes

 module.exports = ctrlFuns;

when I saw the logs at cloud watch then i got:

START RequestId: 720cf48f-01c4-11e9-b715-9d54f664a1e8 Version: $LATEST 2018-12-17T06:24:45.756Z 720cf48f-01c4-11e9-b715-9d54f664a1e8 Error: EROFS: read-only file system, open '/var/task/assets/docs.zip' END RequestId: 720cf48f-01c4-11e9-b715-9d54f664a1e8

with below error message:

{ "errorMessage": "RequestId: 98b9e509-01c7-11e9-94dc-03cfdf0dae93 Process exited before completing request" }

1 Answer 1

60

The error seems self-explanatory:

Error: EROFS: read-only file system, open '/var/task/assets/docs.zip' 

/var/task is where your Lambda function code is located, and in the actual Lambda environment, that filesystem is read-only. If you need to write to a file, you need to write to /tmp.

Q: What if I need scratch space on disk for my AWS Lambda function?

Each Lambda function receives 500MB of non-persistent disk space in its own /tmp directory.

https://aws.amazon.com/lambda/faqs/

Note that you also need to clean up after yourself and remove any temporary files you created, because once a function finishes executing, its container is available for reuse by a later invocation of the same function... which means this same temp space may persist for a short time and be seen again (but only by this same function).

2
  • 3
    I spent many hours trying to fix my code then I realize it was a lambda problem...
    – Gudari
    Nov 17, 2020 at 16:58
  • @Urko I can relate Oct 30, 2022 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.