Getting error AWS Lambda : EROFS: read-only file system, open '/var/task/assets/docs.zip'

别来无恙 提交于 2019-12-11 17:56:20

问题


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:


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:


Lambdas are stateless, you have no access to filesystem of lambda container and all operations should be done in-memory if possible. If it is not possible you can use AWS S3 for example. In your case, as you probably need some files only temporary, you can use S3 Intelligent-Tiering storage type.



来源:https://stackoverflow.com/questions/53810516/getting-error-aws-lambda-erofs-read-only-file-system-open-var-task-assets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!