This shows you the differences between two versions of the page.
— |
addnodejslayertolambda [2021/09/24 00:24] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | = Add a Node.JS Layer to Lambda = | ||
+ | In this article I'll show how to add a Node.JS layer to Lambda. By default, [[https:// | ||
+ | = 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:// | ||
+ | * 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(" | ||
+ | ** As an alternative you can start node by typing {{{node}}} which gives you an prompt to run {{{console.log(" | ||
+ | |||
+ | Now that node runs we can start with the module [[https:// | ||
+ | * We can install the module using {{{npm install paynl-sdk --save}}} | ||
+ | * We can now setup the paynl.js file and test the functionality | ||
+ | |||
+ | <code javascript> | ||
+ | //jshint esversion:6 | ||
+ | |||
+ | console.log(" | ||
+ | var Paynl = require(' | ||
+ | // 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(' | ||
+ | Paynl.Config.setServiceId(' | ||
+ | |||
+ | 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: | ||
+ | <code javascript> | ||
+ | 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 " | ||
+ | ** Name: PaynlTest | ||
+ | ** Runtime: Select the same version you selected for the layer | ||
+ | ** Permissions: | ||
+ | ** 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: | ||
+ | |||
+ | <code javascript> | ||
+ | //jshint esversion:6 | ||
+ | |||
+ | var Paynl = require(' | ||
+ | // 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(' | ||
+ | Paynl.Config.setServiceId(' | ||
+ | |||
+ | exports.handler = (event, context, callback) => { | ||
+ | console.log(" | ||
+ | | ||
+ | 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:// | ||
+ | * [[https:// | ||
+ | |||
+ | {{tag> |