Skip to main content

How to deploy Node.js SDK in AWS Lambda

Question

How to deploy Node.js SDK code in AWS Lambda service?

Answer

Prerequisites:

  1. We will use similar code to the JavaScript SDK example code in this KB Link. Go ahead and download the example and make sure it runs successfully.
  2. AWS Lambda supports Node 10.x as of writing this article, make sure to use that version.

Follow these steps to run Node.js SDK as a Lambda Function:

  1. Create a node project and add the index.js file with the content below, make sure to replace the SDK API KEY, USER ID, and SPLIT NAME with corresponding value from your environment.
const SplitFactory = require('@splitsoftware/splitio').SplitFactory;
const SplitObj = SplitFactory({
core: {
authorizationKey:'SDK API KEY'
},
startup: {
readyTimeout :10
},
scheduler: {
impressionsRefreshRate: 1,
eventsPushRate: 2,
},
debug: true
});

exports.handler = async (event) => {
const client = SplitObj.client();
await client.ready();
var p = new Promise(res => {
var treatment = client.getTreatment("USER ID", "SPLIT NAME");
console.log("\ntreatment: "+treatment);
res(treatment);
});
return await p;
};
  1. Add the dependency libraries for the SDK, run the command below at the root of your project folder:
npm install --save @splitsoftware/splitio@10.16.0
  1. Zip up both index.js and node_modules folder into a new file named function.zip.

  2. Login to AWS, click on the Lambda->Functions link and create new function and use the Author from scratch option, select Node 10.x runtime option, type a function name and click Create function.

  1. Under Basic settings frame, set desired Memory and Timeout. In this example we set the memory to 256 MB and timeout to 1 minute.

  1. Using the AWS cli package, run the command below to upload your function.zip file to the newly created lambda function.
aws lambda update-function-code --function-name split_nodejs --zip-file fileb://function.zip
  1. Configure test event: click on the drop down arrow and select Configure test events item, use the Hello World template to pass key dictionaries to your Lambda function, type a name for your event and click Create.

  1. The Lambda function is now ready to be used, click the Test button to run it, the expected output should be the treatment value, the log output will also show any logging info.