Closed annjawn closed 5 years ago
You're mounting your nodejs
at /opt
– so /opt
will contain the contents of the nodejs
directory.
I suspect you want to be mounting a level up – a few options about how you want to structure this in your directory, but probably best to go with something like this:
home
my-layer-dir
nodejs
config.json
util.js
node_modules
my-lambda-dir
index.js
Then when you run your lambda from my-lambda-dir
do it like this:
docker run --rm \
-v "$PWD":/var/task \
-v "$PWD"/../my-layer-dir:/opt \
lambci/lambda:nodejs8.10 index.handler '{"some": "event"}'
Thanks. Yes I actually tried that but it didn't work. Upon inspecting the image that I have I notice this
"Env": [
"PATH=/usr/local/lib64/node-v4.3.x/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin",
"LD_LIBRARY_PATH=/usr/local/lib64/node-v4.3.x/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib",
"LANG=en_US.UTF-8",
"TZ=:UTC",
"LAMBDA_TASK_ROOT=/var/task",
"LAMBDA_RUNTIME_DIR=/var/runtime",
"_LAMBDA_CONTROL_SOCKET=14",
"_LAMBDA_SHARED_MEM_FD=11",
"_LAMBDA_LOG_FD=9",
"_LAMBDA_SB_ID=7",
"_LAMBDA_CONSOLE_SOCKET=16",
"_LAMBDA_RUNTIME_LOAD_TIME=1530232235231",
"_AWS_XRAY_DAEMON_ADDRESS=xxx.xxx.xx.x",
"_AWS_XRAY_DAEMON_PORT=2000",
"AWS_XRAY_DAEMON_ADDRESS=xxx.xxx.xx.x:2000",
"AWS_XRAY_CONTEXT_MISSING=LOG_ERROR",
"_X_AMZN_TRACE_ID=Parent=11560be54abce8ed",
"AWS_EXECUTION_ENV=AWS_Lambda_nodejs4.3",
"NODE_PATH=/var/runtime:/var/task:/var/runtime/node_modules"
],
I am not using any custom Docker image but shouldn't the NODE_PATH
have /opt/nodejs/node8/node_modules:/opt/nodejs/node_modules
? I just did a docker pull lambci/lambda
to get started.
It does: https://github.com/lambci/docker-lambda/blob/master/nodejs8.10/run/Dockerfile#L6
I'm not sure what you're looking at there? A previous layer in the image?
In any case – what do you mean by "it didn't work"? What was the error?
If you're looking at lambci/lambda:latest
– then that's the wrong image – you should be looking at lambci/lambda:nodejs8.10
Ah yes, sorry my bad, lambci/lambda:nodejs8.10
looks good. Still not able to make it work :(
If your directory structure is as listed above, it should work. Can you show me your directory structure and the error you're getting?
The error says it's looking for ./opt/nodejs/config.json
– not /opt/nodejs/config.json
Dang! sorry, totally my bad. That was the issue.
I am a little confused about the usage of Layers.
The run Dockerfile has
NODE_PATH=/opt/nodejs/node8/node_modules:/opt/nodejs/node_modules:/var/runtime/node_modules:/var/runtime:/var/task:/var/runtime/node_modules
I have defined my layer in
nodejs
directory, and this directory containsnode_modules
with the installed modules, aconfig.json
file, and a util functionutil.js
(The json file and theutil.js
file should be accessible from the function likeconst conf = require('/opt/nodejs/config.json');
and it is working in the Lambda console as such).With this setup when I run this -
docker run --rm -v "$PWD":/var/task -v "$PWD"/nodejs:/opt lambci/lambda:nodejs8.10 index.handler '{"some": "event"}'
it says that
module not found
and alsoconfig.json
not found which tells me that the contents ofnodejs
directory is somehow not mounting to the/opt
directory. If I pull out thenode_modules
and the other files to the root directory where theindex.js
file is and omit the"$PWD"/nodejs:/opt
part, it works fine. Am I missing something here? I really need to be using Layers since all of our Lambdas use Layers and I am documenting setup of our dev environment for developers who need to be using the Layers as well so I am a little lost here.