You cannot load NPM modules without uploading a .zip
file, but you can actually get this process down to two quick command lines.
Here’s how:
Put your Lambda function file(s) in a separate directory. This is because you install
npm
packages locally for Lambda and you want to be able to isolate and test what you will upload to Lambda.Install your NPM packages locally with
npm install packageName
while you’re in your separate Lambda directory you created in step #1.Make sure your function works when running locally:
node lambdaFunc.js
(you can simply comment out the twoexport.handler
lines in your code to adapt your code to run with Node locally).Go to the Lambda’s directory and compress the contents, make sure not to include the directory itself.
zip -r lambdaFunc.zip .
If you have the
aws-cli
installed, which I suggest having if you want to make your life easier, you can now enter this command:aws lambda update-function-code --function-name lambdaFunc \ --zip-file fileb://~/path/to/your/lambdaFunc.zip
(no quotes around the lambdaFunc part above in case you wonder as I did)
Now you can click test in the Lambda console.
I suggest adding a short alias for both of the above commands. Here’s what I have in mine for the much longer Lambda update command:
alias up="aws lambda update-function-code --function-name lambdaFunc \ --zip-file fileb://~/path/to/your/lambdaFunc.zip"