Question
How to deploy NodeJS SDK code in AWS Lambda service?
Answer
Prerequisites:
- 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.
- AWS Lambda support Node 10.x as of writing this article, make sure to use that version.
Follow these steps to run NodeJS 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;
};
2. Add the dependency libraries for Split SDK, run the command below at the root of your project folder:
npm install --save @splitsoftware/splitio@10.16.0
3. Zip up both index.js and node_modules folder into a new file name function.zip
4. Login to AWS, click on Lambda->Functions link and create new function and use Author from scratch option, select Node 10.x Runtime option, type a function name and click Create function.
5. Under Basic settings frame, set desired Memory and Timeout, in this example we set the memory to 256 MB and timeout to 1 minute
5. 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
6. Configure test event: click on the drop down arrow and select Configure test events item, use Hello World template to pass key dictionaries to your Lambda function, type a name for your event and click Create
7. 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.
Comments
0 comments
Please sign in to leave a comment.