

Create a directory under node_modules that contains your custom written packages, and add them to your function, just as you did previously for npm-installed packages: $ echo 'console.log("This is a demo")' > node_modules/bryan/index.js We’ll illustrate with a trivial example that just logs to the console. Custom JavaScript ModulesĮverything you learned above for npm-installing existing modules also applies to custom modules that you create yourself.
#NODE NPM INSTALL MODULES ZIP#
To deploy the resulting function and modules to Lambda, just zip up the entire lambdaTestFunction directory and use Lambda’s createFunction API, CLI, or the console UI to deploy it. You can now delete the test script and continue by creating a real Lambda function that takes advantage of the modules that you’ve just installed, testing it the same way. $ echo 'var AWS = require("aws-sdk") console.log()'> test.jsĪt this point we’ve successfully created a directory containing one or more npm-installed packages and verified that the packages can load and execute by running a test script locally. It’s not interesting in its own right, but it serves to show that the SDK has been installed. We’ll create a trivial test.js file that uses the AWS SDK to print some EC2 config data. Next we’ll run nodejs locally to make sure we have a valid configuration before proceeding.

$ npm install -prefix=~/lambdaTestFunction node_modules/aws-sdkĪt this point we’ve installed the ‘aws-sdk’ module into the node_modules directory of our lambdaTestFunction folder. This is just for illustration the current version of the AWS SDK is pre-installed in Lambda, but you could use this technique to load other pre-built JavaScript packages or if you actually needed an earlier version of the AWS SDK for compatibility reasons.

$ cd lambdaTestFunction Step 2: Install an npm packageįor this example, we’ll keep things simple and install the AWS SDK. Step1: Create a new directory to hold your Lambda function and its modules. We’ll start by including prebuilt modules then move on to native ones. Using npm packages and custom modules/packages with Lambda is easy. Using Existing JavaScript Packages with Lambda To do the steps below, you’ll need an EC2 instance or a similar machine running Amazon Linux with nodejs installed.
#NODE NPM INSTALL MODULES HOW TO#
In this post we take a look at how to use custom nodejs packages with AWS Lambda, including building and packaging native nodejs modules for use in your Lambda functions.
