~1 min read

閱讀繁體中文版 →

Import npm Modules into AWS Lambda Function

When you create a Node.js Lambda function on Amazon Web Services (AWS) and begin editing it using the online editor, you might want to run npm install and import a third-party…

When you create a Node.js Lambda function on Amazon Web Services (AWS) and begin editing it using the online editor, you might want to run npm install and import a third-party library, such as lodash. Unfortunately, there’s no simple way to do this via the web portal.

To accomplish this, you’ll need to write your code in a local environment and then deploy it. First, create a folder on your machine and copy the index.js file into it. Next, run the following commands to initialize your project and install the dependency:

    npm init .
    npm install lodash --save

To use the library in index.js, add the following line:

let _ = require("lodash");

Once you’ve finished writing your code, zip the entire folder, including the node_modules directory, using this command:

    zip -r function.zip .

Finally, deploy the zip file using the AWS CLI tool from your terminal:

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

Replace the yourFunctionName placeholder with the name of your function. If the deployment is successful, you should see "LastUpdateStatus": "Successful" displayed in the terminal, and you can proceed to test the function in the AWS console.

Explore this topic next

  • Deploying a Koa.js Application to an AWS EC2 Ubuntu Instance

    I am developing an application using Koa.js, a new web framework created by the team behind Express. In this step-by-step tutorial, I'll guide you through deploying a Koa.js…

  • Practical AWS FinOps for Cloud Success

    Cloud has fundamentally changed how organizations consume technology. In the traditional IT world, engineers would submit infrastructure requests that flowed through long…

  • Migrating from AWS RDS to Aurora

    Migrating databases is a critical task for any organization looking to enhance performance, scalability, and cost-efficiency. AWS Aurora offers significant benefits over…

Back to all articles