DEV Community

Victor Leung
Victor Leung

Posted on • Originally published at Medium on

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 so, 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
Enter fullscreen mode Exit fullscreen mode

Import your libary to the index.js, such as

let \_ = require('lodash');
Enter fullscreen mode Exit fullscreen mode

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

zip -r function.zip .
Enter fullscreen mode Exit fullscreen mode

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

aws lambda update-function-code --function-name yourFunctionName --zip-file fileb://function.zip
Enter fullscreen mode Exit fullscreen mode

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

Originally published at https://victorleungtw.com.

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.