AWS Lambda Environment Detector

1.0.1 · maintenance · verified Sun Apr 19

is-lambda is a lightweight utility package designed to detect whether the current JavaScript execution environment is an AWS Lambda server. It achieves this by inspecting well-known AWS Lambda-specific environment variables such as `AWS_LAMBDA_FUNCTION_NAME`, `AWS_REGION`, and `LAMBDA_TASK_ROOT`. The current stable version is 1.0.1. Due to its focused and simple nature, its release cadence is typically very slow, with updates primarily occurring if AWS Lambda's environment signature changes or critical bug fixes are required. Its key differentiator is its minimalism, providing a single boolean value without additional overhead or complex configuration, making it suitable for quick environmental checks in serverless applications or conditional logic based on the deployment environment.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `is-lambda` to check the execution environment and shows conditional logic for both Lambda and non-Lambda contexts, including accessing common AWS environment variables.

const isLambda = require('is-lambda');

function main() {
  if (isLambda) {
    console.log('The code is running on an AWS Lambda function.');
    // Example: Accessing Lambda-specific environment variables
    console.log('Function Name:', process.env.AWS_LAMBDA_FUNCTION_NAME ?? 'N/A');
    console.log('AWS Region:', process.env.AWS_REGION ?? 'N/A');
  } else {
    console.log('The code is NOT running on an AWS Lambda function.');
    console.log('Simulating local environment...');
    // Example of local behavior
    const message = 'Hello from local environment!';
    console.log(message);
  }
}

// To demonstrate local behavior when running outside Lambda:
// Temporarily clear AWS Lambda specific environment variables
const originalLambdaEnv = process.env.AWS_LAMBDA_FUNCTION_NAME;
delete process.env.AWS_LAMBDA_FUNCTION_NAME;
main();
process.env.AWS_LAMBDA_FUNCTION_NAME = originalLambdaEnv; // Restore if needed

// To demonstrate Lambda behavior (if run on Lambda, or mock locally):
// process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-test-function';
// process.env.AWS_REGION = 'us-east-1';
// main();

view raw JSON →