epsagon/serverless-plugin-epsagon

Plugin changes the behaviour of "__non_webpack_require__" global function

Closed this issue · 6 comments

One of my app is crashing because I load a JSON file from the filesystem from AWS Lambda, as follow:

const keyFilePath = `../../../credentials.json`;
    logger.info(`Loading file "${keyFilePath}" from "${__filename}" ...`);
    // XXX Be careful, this loads the file within the ".webpack" folder (an not from the "/credentials" folder as we could expect), file must therefore be copied there using webpack - See webpack.config.js
    const keyFile = __non_webpack_require__(keyFilePath); // XXX Use non-webpack because it doesn't support dynamic path - See https://github.com/webpack/webpack/issues/4175#issuecomment-277232067

This used to work fine, but it fails when I use this plugin with Cannot find module '../../../credentials.json'

I therefore believe the plugin changes the behaviour of __non_webpack_require__ somehow.
This is breaking change for me, and forces me to use https://github.com/epsagon/epsagon-node instead

Using 1.6.1

Well, actually https://github.com/epsagon/epsagon-node causes the same side effect. It's therefore rather global to Epsagon rather than plugin-specific.

Okay, understood the issue.
It’s because it loads the file relatively to the handler
and my epsagon handler is not at the same level as my normal handler
but it’s very hard to figure out since files aren’t really visible on AWS

I changed my source code and used absolute paths instead of relative ones.

const prefixPath = process.env.LAMBDA_TASK_ROOT ? process.env.LAMBDA_TASK_ROOT : '../../..'; // XXX Use relative path when running locally
    logger.debug(`prefixPath="${prefixPath}"`);

    const keyFilePath = `${prefixPath}/credentials.json`;

This way, there is no path change depending on how is bundled the zip and how it's executed on AWS. I used LAMBDA_TASK_ROOT to know the absolute path of the lambda working directory. When working locally, I rely on relative paths because it's easier to reason about that way.

Reference for LAMBDA_TASK_ROOT: https://docs.aws.amazon.com/fr_fr/lambda/latest/dg/lambda-environment-variables.html

I believe a warning should be added in the documentation though, since it took me around 3h to find the root cause and find a proper fix.

Hi @Vadorequest, Thanks for reporting! We will dive into the details to see what we can improve to prevent this inconvenience!

🎉 This issue has been resolved in version 1.6.2 🎉

The release is available on:

Your semantic-release bot 📦🚀