How to load npm modules in AWS Lambda?

Amazon Web-ServicesNpmAws Lambda

Amazon Web-Services Problem Overview


I've created several Lambda functions using the web based editor. So far so good. I'd now like to start extending those with modules (such as Q for promises). I can't figure out how to get the modules out to Lambda so they can be consumed by my functions.

I've read through this but it seems to involve setting up an EC2 and running Lambda functions from there. There is a mechanism to upload a zip when creating a function but that seems to involve sending up functions developed locally. Since I'm working in the web based editor that seems like a strange workflow.

How can I simply deploy some modules for use in my Lambda functions?

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

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:

  1. 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.

  2. Install your NPM packages locally with npm install packageName while you're in your separate Lambda directory you created in step #1.

  3. Make sure your function works when running locally: node lambdaFunc.js (you can simply comment out the two export.handler lines in your code to adapt your code to run with Node locally).

  4. Go to the Lambda's directory and compress the contents, make sure not to include the directory itself.

     zip -r lambdaFunc.zip .
    
  5. 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)

  6. Now you can click test in the Lambda console.

  7. 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"
    

Solution 2 - Amazon Web-Services

A .zip file is required in order to include npm modules in Lambda. And you really shouldn't be using the Lambda web editor for much of anything- as with any production code, you should be developing locally, committing to git, etc.

MY FLOW:
  1. My Lambda functions are usually helper utilities for a larger project, so I create a /aws/lambdas directory within that to house them.

  2. Each individual lambda directory contains an index.js file containing the function code, a package.json file defining dependencies, and a /node_modules subdirectory. (The package.json file is not used by Lambda, it's just so we can locally run the npm install command.)

package.json:

{
  "name": "my_lambda",
  "dependencies": {
    "svg2png": "^4.1.1"
  }
}

3) I .gitignore all node_modules directories and .zip files so that the files generated from npm installs and zipping won't clutter our repo.

.gitignore:

# Ignore node_modules
**/node_modules

# Ignore any zip files
*.zip

4) I run npm install from within the directory to install modules, and develop/test the function locally.

  1. I .zip the lambda directory and upload it via the console.

(IMPORTANT: Do not use Mac's 'compress' utility from Finder to zip the file! You must run zip from the CLI from within the root of the directory- see here)

zip -r ../yourfilename.zip * 

NOTE:

You might run into problems if you install the node modules locally on your Mac, as some platform-specific modules may fail when deployed to Lambda's Linux-based environment. (See https://stackoverflow.com/a/29994851/165673)

The solution is to compile the modules on an EC2 instance launched from the AMI that corresponds with the Lambda Node.js runtime you're using (See this list of Lambda runtimes and their respective AMIs).


See also AWS Lambda Deployment Package in Node.js - AWS Lambda

Solution 3 - Amazon Web-Services

You can now use Lambda Layers for this matters. Simply add a layer containing the package you need and it will run perfectly.

Follow this post: https://medium.com/@anjanava.biswas/nodejs-runtime-environment-with-aws-lambda-layers-f3914613e20e

Solution 4 - Amazon Web-Services

Hope this helps, with Serverless framework you can do something like this:

  1. Add these things in your serverless.yml file:
    plugins:
      - serverless-webpack
    custom:
      webpackIncludeModules:
        forceInclude:
          - <your package name> (for example: node-fetch)
  1. Then create your Lambda function, deploy it by serverless deploy, the package that included in serverless.yml will be there for you.

For more information about serverless: https://serverless.com/framework/docs/providers/aws/guide/quick-start/

Solution 5 - Amazon Web-Services

After fiddling around with parcel for a few hours, I found that it seems to make some assumptions about running in a browser (even if I tell it to use engine: node).

Instead:


esbuild

Much easier, and also faster, is esbuild!

Simply run npm add --save-dev esbuild, and add these scripts to your package.json:

{
  ...
  "scripts": {
    "build": "esbuild --bundle --minify --platform=node --target=node12 --outdir=build main.js",
    "export": "cd build && zip main.js.zip main.js"
  },
  ...
  "devDependencies": {
    "esbuild": "^0.11.19",
    ...
  }
}

This allowed me to use the aws-sdk while still getting tree-shaking and minifying, while still being able to install other dependencies such as jest and eslint without having to package the whole node_modules folder.

To build a package within CI, simply: npm ci && npm run build && npm run export

The file build/main.js.zip will then contain everything you need!

Solution 6 - Amazon Web-Services

Also in the many IDEs now, ex: VSC, you can install an extension for AWS and simply click upload from there, no effort of typing all those commands + region.

Here's an example:

enter image description here

Solution 7 - Amazon Web-Services

npm module has to be bundeled inside your nodejs package and upload to AWS Lambda Layers as zip, then you would need to refer to your module/js as below and use available methods from it. const mymodule = require('/opt/nodejs/MyLogger');

Solution 8 - Amazon Web-Services

This is an old-ish question, but it helped lead me to a really easy way of adding new Lambda dependencies to an Alexa skill.

Like JohnAllen's answer, you need to create a folder on your local machine, title it whatever you want (it's arbitrary):

mkdir lambdaFunc
cd lambdaFunc

Once in your folder, use npm to install the necessary package. For me, I needed to parse ISO8601 durations (my command was npm install iso8601-duration):

npm install <your-package-here>

Once installed, back out of that directory, and zip it. Open up your Alexa Skill in the Alexa Skill developer console, then select the "Import Code" option. From here, you'll upload your .zip file, and select all the code:

Import Lambda Code in Alexa Skill Developer Console

Select all code in .zip file

That's it! Then you can just import the code, like I did:

const DateConverter = require('iso8601-duration');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFookView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesJohnAllenView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesYarinView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesEldad HauzmanView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesToaiView Answer on Stackoverflow
Solution 5 - Amazon Web-ServicescfstrasView Answer on Stackoverflow
Solution 6 - Amazon Web-ServicesElKePoNView Answer on Stackoverflow
Solution 7 - Amazon Web-ServicesSunil ChaurahaView Answer on Stackoverflow
Solution 8 - Amazon Web-ServicesConcernedHobbitView Answer on Stackoverflow