Short answer:
AWS Lambda functions handler.js build / spawned by a Serverless Framework will be placed at the path: /var/task/src/hander.js
The default runtime directory for Lambda (at least Node8.10) is:
/var/task
Long Answer: This was about webpack including JSON files.
I was really asking the wrong question to achieve my desired result. What I really wanted to know was:
How do I tell webpack to include a json file in my Serverless Framework Lambda function — so that the lambda can access that json file at run time?
In your repo that /src directory (probably — if you are using the default Serverless project structure) contains your handler.js file and is the reference point (first directory to be copied in from your serverless project) from your Repo — nothing above that src directory is copied in.
That said trying assuming that your local file / folder structure looks like this:
repo-root
——— src
——— ——— someFunction
——— ——— ——— some-sub-directory
It is important to remember that the serverless AWS Lambda file structure will look like this (by default):
——— src
——— ——— handler.js
——— node_modules
Notice there is nothing else there (by default) beyond your handler.js file.
No subdirectories no nothing. This is because webpack has not been told to include the JSON file (which takes some work).
Correct Serverless Lambda Absolute Path via '__dirname'
The first thing that is required is to include / require / reference your json file from within your code base. This lets webpack know that you will need it later:
let file = require('./some-sub-directory/targetfile.json');
If you don't properly require the JSON file it won't make a difference what location you point to it won't be there. Another more 'Hacky' option would be to just rename your JSON file to .js and then include via the same process.
Webpack File-loader Config
Just including the JSON file (in my case) did not trigger webpack to actually include the file in the bundler. I also needed to add the webpack file-loader: https://www.npmjs.com/package/file-loader
And then add the following to my webpack config — under rules (it would be slightly different for webpack 4):
{
// type: 'javascript/auto', // this would be for webpack 4
test: /\.json$/,
use: [
{
loader: 'file-loader',
options: {
name: "./src/mypackedfiles/[name].[ext]"
}
}
]
}
More details can be found over here:
https://github.com/webpack/webpack/issues/6586#issuecomment-379448638
Keep in mind that just including the file DOES NOT parse the values that are enclosed in that file — it only tells webpack to include it so that you can then read it locally from the Lambda using fs
or some other method.
Trouble Shooting (locating the file)
The key here (if you were trying to get a json file included / accessible to a Serverless AWS Lambda function like I was) is to realize that this is really more of a question of webpack than it is of Lambda pathing.
For that reason the best way to trouble shoot / find your file is just to build your function and then un-zip the file it creates to see if your json has been properly included.
Once it has been included — THEN you can use the above mentioned settings with __dirname to track down and actually utilize that file.
Final JSON File Location within Serverless Lambda
Based on the above webpack configuration your directory structure (within your zip) would look like this:
——— src
——— ——— mypackedfiles
——— ——— ——— targetfile.json
——— ——— someFunction
——— ——— ——— some-sub-directory
——— node_modules
So with that in mind your reference to the json file added by webpack would now look like this:
let filelocation = __dirname + '/mypackedfiles/targetfile.json'
You could now access filelocation from fs
or whatever parser you want as it is properly included in your serverless lambda.
Note for Multiple Functions within one Repo: With this method the json file will be copied into EVERY function that is spawned by your repo. This may or may not be a good thing for you but it works fine for me since my JSON files are not overly huge.