AWS Lambda error - No such file or directory

6k views Asked by At

I've deployed some code a AWS lambda and I am getting the following error:

ENOENT No such file or directory found var/task/../data/cacert.pem

The file it's looking for is within the lambda see image below:

enter image description here

I used webpack to add the file there :

{
   from: 'node_modules/tinify/lib/data/cacert.pem', to: 'app/../data/cacert.pem'
}

Can anyone help me understand whats causing this?

Edit:

The certificate file is being imported within the handler.js file see below:

let data = fs.readFileSync(__dirname "/../data/cacert.pem").toString()

Please note that the above code is added by webpack when it bundles the app and I can't edit that import statement. The certificate is needed by aa third party library (tinify.js).

1

There are 1 answers

2
stijndepestel On

You've got an error in the code that is importing the certificate. You're not constructing the path correctly.

You should use the following:

const path = require('path');
const fs = require('fs');

const cert = fs.readFileSync(path.join(__dirname, './data/cacert.pem')).toString();

path.join will construct the correct path based on the location of the handler file and the relative path between the handler and the certificate.