lambci / docker-lambda

Docker images and test runners that replicate the live AWS Lambda environment
MIT License
5.83k stars 431 forks source link

Difficulties with NodeJS8.10 Layers Configuration #173

Closed annjawn closed 5 years ago

annjawn commented 5 years ago

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 contains node_modules with the installed modules, a config.json file, and a util function util.js (The json file and the util.js file should be accessible from the function like const 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 also config.json not found which tells me that the contents of nodejs directory is somehow not mounting to the /opt directory. If I pull out the node_modules and the other files to the root directory where the index.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.

mhart commented 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"}'
annjawn commented 5 years ago

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.

mhart commented 5 years ago

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?

mhart commented 5 years ago

If you're looking at lambci/lambda:latest – then that's the wrong image – you should be looking at lambci/lambda:nodejs8.10

annjawn commented 5 years ago

Ah yes, sorry my bad, lambci/lambda:nodejs8.10 looks good. Still not able to make it work :(

mhart commented 5 years ago

If your directory structure is as listed above, it should work. Can you show me your directory structure and the error you're getting?

annjawn commented 5 years ago

Screen Shot 2019-03-19 at 4 57 45 PM

mhart commented 5 years ago

The error says it's looking for ./opt/nodejs/config.json – not /opt/nodejs/config.json

annjawn commented 5 years ago

Dang! sorry, totally my bad. That was the issue.