= Add a Node.JS Layer to Lambda =
**Summary**: How to Add a Node.JS layer to AWS Lambda. \\
**Date**: Around 2022 \\
**Refactor**: 20 February 2025: Checked links and formatting. \\
{{tag>aws nodejs lambda}}
In this article I'll show how to add a Node.JS layer to Lambda. By default, [[https://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html |Lambda supports Node.JS]] [[https://nodejs.org/docs/latest-v12.x/api/index.html |12]] and [[ https://nodejs.org/docs/latest-v10.x/api/index.html |10]] native modules and the [[https://aws.amazon.com/sdk-for-node-js/ |AWS SDK for JavaScript in Node.JS]] (subject to change). However, there are many modules for Node.JS available that extend the possibilities. I'll show how to add the Node.JS module paynl to Lambda.
= First play with Paynl =
If you're just interested in the layer stuff you can skip this step. I find it useful however to first test a module natively on my workstation before adding it to Lambda:
* First download ans install the LTS (long-term-support) or latest version from [[https://nodejs.org/en/ |nodejs.org]]. I recommend to install the Long Term Support version, which is 12.18.3 right now. During the install you can keep all the installation defaults.
* After the installation you have to restart your computer and you can test the installation with {{{node --version}}} from a terminal (like powershell)
* If you want to test a bit more you could create a file called {{{index.js}}} with the text {{{console.log("Hello world")}}} and run it with {{{node index.js}}}
* As an alternative you can start node by typing {{{node}}} which gives you an prompt to run {{{console.log("Hello world")}}} directly
Now that node runs we can start with the module [[https://github.com/paynl/nodejs-sdk |paynl]]:
* We can install the module using {{{npm install paynl-sdk --save}}}
* We can now setup the paynl.js file and test the functionality
//jshint esversion:6
console.log("Start testing with paynl node.js module");
var Paynl = require('paynl-sdk');
// In the pay.nl admin, navigate to Manage -> Services and click the SL-code on the left.
// From the popup use the apitoken and serviceId, and configure them in the SDK.
Paynl.Config.setApiToken('xxxxx');
Paynl.Config.setServiceId('xxxxx');
Paynl.Paymentmethods.getList().forEach(
function(paymentmethod) {
console.log(paymentmethod.id + ' ' + paymentmethod.visibleName);
}
)
.catch(error => {
console.error(error);
});
Start the script (after setting your own token and serviceId) using:
node paynl.js
Which should output something like this:
Start testing with paynl node.js module
10 iDEAL
Now that we know we have a working module and script we can start creating a Layer for Lambda so we can run the script from Lambda.
= Add Layer in Lambda =
Follow the following steps to add the paynl module as a layer to Lambda
* First create an empty directory called {{{nodejs}}} (this must be nodejs)
* From inside of the directory issue the following command to initialize npm {{{npm init}}} which will start a wizard to create a package.json file
* Now install the paynl module: {{{npm install --save paynl-sdk}}}
* Now zip the nodejs directory to create a nodejs.zip file
* Now move to the AWS Lambda console and click Layers under Additional resources
* Click on Create Layer and provide:
* Name: nodejs-paynl
* Upload zipfile
* Set the latest version of node as the runtime
Now that we've added a new layer to Lambda we can add it to a Lambda Function.
= Add Layer to Lambda Function =
Follow the following steps to create a Lambda Function using the script above and adding the layer to it:
* Still inside the AWS Lambda console, click on Functions to access the Lambda Functions
* Click on Create Function and select "Author from scratch":
* Name: PaynlTest
* Runtime: Select the same version you selected for the layer
* Permissions: If you don't have one yet choose to "Create a new role with Basic Lambda permissions" or use an existing role with the required permissions already assigned
* Click on Create Function
* Inside the new function, click on Layers and click "Add a Layer"
* Select Custom Layers and select your created layer from the drop down menu
* Select the available version from the dropdown menu
* Click Add
* Inside the function, scroll down to Function code, and replace the default code with the code below and click Save:
//jshint esversion:6
var Paynl = require('paynl-sdk');
// In the pay.nl admin, navigate to Manage -> Services and click the SL-code on the left.
// From the popup use the apitoken and serviceId, and configure them in the SDK.
Paynl.Config.setApiToken('xxxxx);
Paynl.Config.setServiceId('xxxxx');
exports.handler = (event, context, callback) => {
console.log("Start testing with paynl node.js module");
Paynl.Paymentmethods.getList().forEach(
function(paymentmethod) {
console.log(paymentmethod.id + ' ' + paymentmethod.visibleName);
}
)
.catch(error => {
console.error(error);
});
};
Now we are finished we can check the function:
* Still inside the new created Function, click Test at the top
* Provide a name for the test event, and you can keep the default input as this is not used in the function
* Click Create
* Once the test event is created click Test again
Now, part of the output should be something like this:
INFO Start testing with paynl node.js module
INFO 10 iDEAL
= Resources =
* [[https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html|AWS Lambda Layers]]
* [[https://medium.com/@anjanava.biswas/nodejs-runtime-environment-with-aws-lambda-layers-f3914613e20e| Lambda Layers howto]]