Skip to main content

Deploy Node.js SDK in AWS Lambda

Last updated on

Overview

This page provides information on how to deploy Node.js SDK code in AWS Lambda services.

Before you begin

  • Use similar Node.js SDK code as in the JavaScript SDK example.
  • AWS Lambda supports Node 10.x runtime (as of writing).
  • Make sure your environment uses Node 10.x or compatible version.

Step-by-step Guide

  1. Create your Node.js project

    Add an index.js file with the following content. Replace the placeholders SDK API KEY, USER ID, and SPLIT NAME with your actual values:

    const SplitFactory = require('@splitsoftware/splitio').SplitFactory;

    const factory = SplitFactory({
    core: {
    authorizationKey: 'SDK API KEY'
    },
    startup: {
    readyTimeout: 10
    },
    scheduler: {
    impressionsRefreshRate: 1,
    eventsPushRate: 2,
    },
    debug: true
    });

    exports.handler = async (event) => {
    const client = factory.client();
    try {
    await client.whenReady();
    } catch (error) {
    console.log("SDK_READY_TIMED_OUT event emitted. Treatments will be control");
    }

    const treatment = client.getTreatment("USER ID", "SPLIT NAME");
    console.log("\ntreatment: " + treatment);
    return treatment;
    };
  2. Install the SDK dependency

    Run this command at your project root to install the Node.js SDK package:

    npm install --save @splitsoftware/splitio@10.16.0
  3. Prepare deployment package

    Zip the index.js file and the node_modules folder into function.zip.

  4. Create your Lambda function

    • Login to AWS Console
    • Go to LambdaFunctions
    • Click Create function
    • Choose Author from scratch
    • Select Node.js 10.x runtime
    • Give your function a name
    • Click Create function
  5. Configure Lambda function settings

    • Under Basic settings, set Memory (e.g., 256 MB)
    • Set Timeout (e.g., 1 minute)
  6. Upload your deployment package

    Using the AWS CLI, upload your zipped package:

    aws lambda update-function-code --function-name YOUR_FUNCTION_NAME --zip-file fileb://function.zip
  7. Configure test event

    • In the Lambda console, select Configure test events
    • Use the Hello World template
    • Provide necessary event key-value data (if needed)
    • Save the test event
  8. Test your Lambda function

    • Click Test
    • The expected output should be the treatment value
    • Logs will show any additional debug information

This example demonstrates how to run the Split Node.js SDK within AWS Lambda and retrieve feature flag treatments programmatically.