AWS Lambda Samples (Nomad Media SDK Sync)

How to run Nomad Media SDK content sync samples inside AWS Lambda functions, with JavaScript and Python walkthroughs.

This guide walks through setting up the Nomad Media SDK sync samples to run inside an AWS Lambda function. The samples demonstrate syncing content definitions (e.g., a movie catalog) between an external JSON data source and Nomad Media.

Both JavaScript (Node.js 20.x) and Python 3.12 implementations are covered. Each follows the same general steps: create a secret, create an IAM policy and role, create the Lambda, import the SDK, and wire up the sync sample.


Prerequisites

  • Python setup: See Python Lambda Setup before starting the Python section.
  • For both languages, an AWS Secrets Manager secret is required for Nomad Media credentials.

Step 1 — Create the AWS Secret

  1. Go to AWS Secrets ManagerStore a new secret.
  2. Select Other type of secret.
  3. Add your Nomad Media username and password as key/value pairs and click Next.
  4. Enter a secret name and click through to Store.
  5. Open the secret and copy the Secret ARN — you'll need it in Step 2.

Step 2 — Create the IAM Policy

  1. Go to IAMPoliciesCreate policy.
  2. Select Secrets Manager as the service.
  3. Under Actions allowedRead, select GetSecretValue.
  4. Under ResourcesSpecific, click Add ARNs and paste the ARN from Step 1.
  5. Click Next, enter a policy name, and click Create policy.

Step 3 — Create the IAM Role

  1. Go to IAMRolesCreate role.
  2. Select AWS serviceLambda as the use case. Click Next.
  3. Find and select the policy created in Step 2. Click Next.
  4. Enter a role name and click Create role.

Step 4 — Create the Lambda

  1. Go to LambdaCreate function.
  2. Enter a function name.
  3. Select Node.js 20.x (JavaScript) or Python 3.12 (Python) as the Runtime.
  4. Under Change default execution role, select Use an existing role and choose the role from Step 3.
  5. Click Create function.

JavaScript Setup

Import the Nomad Media NPM Package

  1. Create a new directory locally.
  2. Create a package.json file with {} as its contents.
  3. Run:
    npm install @nomad-media/full --save
    npm install fs --save
  4. Zip the directory.
  5. In your Lambda Code tab, click Upload from.zip file, select your zip, and click Save.
  6. Move the three package files from the subfolder to the top-level directory in the Lambda editor.

Initialize the SDK with Secrets

Add the following to your Lambda code to retrieve credentials from Secrets Manager and initialize the SDK:

import { SecretsManagerClient, GetSecretValueCommand } from "@aws-sdk/client-secrets-manager";
import { fromEnv } from "@aws-sdk/credential-provider-env";
import NomadMediaSDK from "nomad-media-npm";

const SECRET_NAME = "my-secret";   // do not include the trailing alphanumerics
const REGION_NAME = "us-west-2";   // your AWS region

const SECRETS_MANAGER = new SecretsManagerClient({
    region: REGION_NAME,
    credentials: fromEnv(),
});

async function getSecret() {
    const command = new GetSecretValueCommand({ SecretId: SECRET_NAME });
    try {
        const data = await SECRETS_MANAGER.send(command);
        return JSON.parse(data.SecretString);
    } catch (error) {
        console.error(error);
    }
}

const SECRETS = await getSecret();

const config = {
    username: SECRETS.user,
    password: SECRETS.password,
    serviceApiUrl: "YOUR_SERVICE_API_URL",
    apiType: "admin",
    debugMode: false
};

const NomadSDK = new NomadMediaSDK(config);

Sample Sync Code (JavaScript)

Create index.mjs with a sync function that reads from a local movies.json source file and synchronizes records into Nomad Media content definitions:

const GENRE_CONTENT_DEFINITION_ID = "dbbace1f-ddb1-462b-9cae-c9be7d5990ac";
const MOVIE_CONTENT_DEFINITION_ID = "eb710e28-7c44-492e-91f9-8acd0cd9331c";

// Replace these IDs with the content definition IDs from your environment

async function sync() {
    const MOVIES = await get_movies();
    const JSON_MOVIES = JSON.parse(fs.readFileSync('movie.json', 'utf8'));

    for (let index = 0; index < JSON_MOVIES.length; index++) {
        const MOVIE = MOVIES.find(m => m.identifiers?.movieId === JSON_MOVIES[index].id) || null;
        await check_movie(index, MOVIE, JSON_MOVIES[index], JSON_MOVIES.length);
    }
}

See the full sample code in the Nomad Media samples repository.

Call sync() from your Lambda handler function, then click Deploy and use the Test button to verify. If you receive a timeout error, increase the timeout in ConfigurationGeneral configurationEditTimeout.


Python Setup

Add the Lambda Layer

  1. In the nomad-samples directory of the sample code, locate the nomad_media_sdk_pip.zip file.
  2. Go to LambdaLayersCreate layer.
  3. Enter a name, upload the zip, and select Python 3.12 as the compatible runtime. Click Create.
  4. Open your Lambda function → Code tab → scroll to LayersAdd a layer.
  5. Select Custom layers, choose the layer you created, and click Add.

Initialize the SDK with Secrets

import boto3
import json

secret_name = ""    # do not include trailing alphanumerics
region_name = ""    # your AWS region (e.g., "us-west-2")

session = boto3.session.Session()
client = session.client(service_name="secretsmanager", region_name=region_name)

get_secret_value_response = client.get_secret_value(SecretId=secret_name)
secret = json.loads(get_secret_value_response["SecretString"])

config = {
    "username": secret["user"],
    "password": secret["password"],
    "serviceApiUrl": "YOUR_SERVICE_API_URL",
    "apiType": "admin",
    "debugMode": False
}

Sample Sync Code (Python)

  1. Unzip samples-main.zip.
  2. Copy the sync sample from samples-main/nomad-samples/admin/py/sync1.1 and paste it above the Lambda handler.
  3. Add movie.json and delete_ids.py as separate files in the Lambda editor.
  4. Replace the main() function body inside the Lambda handler.
  5. Replace any absolute file paths with relative paths.
  6. Update content definition ID constants to match your environment.

Click Deploy and use Test to verify. Increase the timeout under ConfigurationGeneral configuration if you see timeout errors.