Import npm modules to AWS lambda function

When you create a node.js lambda function on Amazon Web Service (AWS), and start editing using the online editor, then you want to npm install and import a third party library, such as lodash, unfortunately there is no simple way to do it via the web portal.

In order to do it, you need to write your code in local environment and deploy it. First, create a folder in your machine and copy the index.js file inside it. Then run the below command to init and install your dependency:

npm init .
npm install lodash --save

Import your libary to the index.js, such as

let _ = require('lodash');

After you finish writing your code, zip the entire folder including those node_modules via this command:

zip -r .

Finally, deploy the zip file using AWS CLI tools in terminal:

aws lambda update-function-code --function-name yourFunctionName --zip-file fileb://

Replace yourFunctionName placeholder with your function name. If it’s deploy ok, you should see “LastUpdateStatus”: “Successful” and you can continue the testing in the console.

By Victor Leung

Experience in software development, consulting services and technical product management. Understanding of business and technology with an MBA in Finance and a Master degree in Computer Science. AWS Certified Solution Architect with experience in building products from scratch and serving as a charismatic leader.

Leave a comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: