or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

aws-sdk-integration.mdbase64.mddynamodb.mdenvironment-variables.mdindex.mdlru-cache.mdmiddy-integration.mdtype-utilities.mdutility-class.md
tile.json

environment-variables.mddocs/

Environment Variable Utilities

Safely read and validate environment variables with type conversion, default value support, and automatic error handling. Essential for configuring Lambda functions through environment variables.

Capabilities

Get String from Environment

Get a string value from environment variables with validation and optional default.

/**
 * Get a string from the environment variables.
 * By default, the value is trimmed and always required unless a default is provided.
 *
 * @param options - Configuration options
 * @returns The environment variable value as a trimmed string
 * @throws Error if variable not found and no default provided
 */
function getStringFromEnv(options: GetStringFromEnvOptions): string;

interface GetStringFromEnvOptions {
  /** The key of the environment variable */
  key: string;
  /** Optional default value to return if the environment variable is not set */
  defaultValue?: string;
  /** Optional error message to throw if the environment variable is not set and no default provided */
  errorMessage?: string;
}

Usage Examples:

import { getStringFromEnv } from '@aws-lambda-powertools/commons/utils/env';

// Required environment variable with custom error message
const apiKey = getStringFromEnv({
  key: 'API_KEY',
  errorMessage: 'API_KEY is required for this function',
});

// With default value
const region = getStringFromEnv({
  key: 'AWS_REGION',
  defaultValue: 'us-east-1',
});

Get Number from Environment

Get a numeric value from environment variables with automatic parsing and validation.

/**
 * Get a number from the environment variables.
 * The value is trimmed before being converted to a number and always required unless a default is provided.
 *
 * @param options - Configuration options
 * @returns The environment variable value as a number
 * @throws Error if variable not found and no default provided
 * @throws TypeError if the value cannot be parsed as a number
 */
function getNumberFromEnv(options: GetNumberFromEnvOptions): number;

interface GetNumberFromEnvOptions {
  /** The key of the environment variable */
  key: string;
  /** Optional default value to return if the environment variable is not set */
  defaultValue?: number;
  /** Optional error message to throw if the environment variable is not set and no default provided */
  errorMessage?: string;
}

Usage Examples:

import { getNumberFromEnv } from '@aws-lambda-powertools/commons/utils/env';

// Required number with error handling
const timeout = getNumberFromEnv({
  key: 'TIMEOUT_MS',
  errorMessage: 'TIMEOUT_MS must be set',
});

// With default value
const maxRetries = getNumberFromEnv({
  key: 'MAX_RETRIES',
  defaultValue: 3,
});

Get Boolean from Environment

Get a boolean value from environment variables with flexible parsing options.

/**
 * Get a boolean from the environment variables.
 * The value is trimmed before being converted to a boolean and always required unless a default is provided.
 *
 * Standard parsing accepts 'true' and 'false'.
 * Extended parsing accepts: '1', 'y', 'yes', 't', 'true', 'on' for true
 * and '0', 'n', 'no', 'f', 'false', 'off' for false (case-insensitive).
 *
 * @param options - Configuration options
 * @returns The environment variable value as a boolean
 * @throws Error if variable not found and no default provided, or if value is not a valid boolean
 */
function getBooleanFromEnv(options: GetBooleanFromEnvOptions): boolean;

interface GetBooleanFromEnvOptions {
  /** The key of the environment variable */
  key: string;
  /** Optional default value to return if the environment variable is not set */
  defaultValue?: boolean;
  /** Optional error message to throw if the environment variable is not set and no default provided */
  errorMessage?: string;
  /** Whether to extend parsing to include common string representations ('1', 'yes', 'on', etc.) */
  extendedParsing?: boolean;
}

Usage Examples:

import { getBooleanFromEnv } from '@aws-lambda-powertools/commons/utils/env';

// Standard boolean parsing
const debugMode = getBooleanFromEnv({
  key: 'DEBUG_MODE',
  defaultValue: false,
});

// Extended parsing for flexible input
const isEnabled = getBooleanFromEnv({
  key: 'FEATURE_ENABLED',
  defaultValue: true,
  extendedParsing: true,
});
// Accepts: 'yes', '1', 'on', 'true', etc.

Check Development Mode

Check if the current invocation is running in development environment.

/**
 * Check if the current invocation is running in a development environment.
 * This is determined by the POWERTOOLS_DEV environment variable.
 * Returns false if the variable is not set or cannot be parsed.
 *
 * @returns Boolean indicating if running in development mode
 */
function isDevMode(): boolean;

Usage Example:

import { isDevMode } from '@aws-lambda-powertools/commons/utils/env';

if (isDevMode()) {
  console.log('Running in development mode');
  // Enable additional debugging, verbose logging, etc.
}

Get Service Name

Get the service name from the POWERTOOLS_SERVICE_NAME environment variable.

/**
 * Get the service name from the environment variables.
 * This is determined by the POWERTOOLS_SERVICE_NAME environment variable.
 * Returns empty string if not set.
 *
 * @returns The service name or empty string
 */
function getServiceName(): string;

Usage Example:

import { getServiceName } from '@aws-lambda-powertools/commons/utils/env';

const serviceName = getServiceName();
console.log('Service:', serviceName);

Get X-Ray Trace Data

Get the AWS X-Ray trace data from the Lambda RIC async context or the _X_AMZN_TRACE_ID environment variable.

/**
 * Get the AWS X-Ray Trace data from the lambda RIC async context or the _X_AMZN_TRACE_ID environment variable.
 * Checks the async context first and if that returns undefined, falls back to the environment variable.
 * The method parses the value and returns an object with the key-value pairs.
 *
 * @returns Record containing trace data (Root, Parent, Sampled) or undefined if not available
 */
function getXrayTraceDataFromEnv(): Record<string, string> | undefined;

Usage Example:

import { getXrayTraceDataFromEnv } from '@aws-lambda-powertools/commons/utils/env';

const traceData = getXrayTraceDataFromEnv();
if (traceData) {
  console.log('Trace Root:', traceData.Root);
  console.log('Parent:', traceData.Parent);
  console.log('Sampled:', traceData.Sampled);
}

Check X-Ray Sampling

Determine if the current invocation is part of a sampled X-Ray trace.

/**
 * Determine if the current invocation is part of a sampled X-Ray trace.
 * The AWS X-Ray Trace data has format: 'Root=1-xxx;Parent=xxx;Sampled=1'
 * Checks the async context first and if that returns undefined, falls back to the environment variable.
 *
 * @returns Boolean indicating if the request is sampled (Sampled=1)
 */
function isRequestXRaySampled(): boolean;

Usage Example:

import { isRequestXRaySampled } from '@aws-lambda-powertools/commons/utils/env';

if (isRequestXRaySampled()) {
  console.log('Request is being traced by X-Ray');
  // Add additional tracing information
}

Get X-Ray Trace ID

Get the AWS X-Ray Trace ID from the Lambda RIC async context or environment variable.

/**
 * AWS X-Ray Trace ID from the lambda RIC async context or the _X_AMZN_TRACE_ID environment variable.
 * Checks the async context first and if that returns undefined, falls back to the environment variable.
 * The AWS X-Ray Trace data has format: 'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=xxx;Sampled=1'
 * The actual Trace ID is: '1-5759e988-bd862e3fe1be46a994272793'
 *
 * @returns The X-Ray trace ID or undefined if not available
 */
function getXRayTraceIdFromEnv(): string | undefined;

Usage Example:

import { getXRayTraceIdFromEnv } from '@aws-lambda-powertools/commons/utils/env';

const traceId = getXRayTraceIdFromEnv();
if (traceId) {
  console.log('Current trace ID:', traceId);
}

Check Invoke Store Usage

Check if AWS_LAMBDA_MAX_CONCURRENCY environment variable is set, indicating if invoke store should be used.

/**
 * Check if the AWS_LAMBDA_MAX_CONCURRENCY environment variable is set.
 * This indicates whether the invoke store should be used.
 *
 * @returns Boolean indicating if invoke store should be used
 */
function shouldUseInvokeStore(): boolean;

Usage Example:

import { shouldUseInvokeStore } from '@aws-lambda-powertools/commons/utils/env';

if (shouldUseInvokeStore()) {
  console.log('Using invoke store for context management');
}