UNPKG

failure-lambda

Version:

Failure injection for AWS Lambda - chaos engineering made simple

68 lines (65 loc) 2.89 kB
import { Context, Callback } from 'aws-lambda'; /** The supported failure injection modes */ type FailureMode = "latency" | "exception" | "statuscode" | "diskspace" | "denylist" | "timeout" | "corruption"; /** Match operators for event-based targeting */ type MatchOperator = "eq" | "exists" | "startsWith" | "regex"; /** Condition for event-based targeting */ interface MatchCondition { /** Dot-separated path into the event object (e.g. "requestContext.http.method") */ path: string; /** Expected string value at the path. Required for all operators except "exists". */ value?: string; /** Comparison operator. Defaults to "eq". */ operator?: MatchOperator; } /** A single feature flag's value */ interface FlagValue { enabled: boolean; /** Percentage of invocations to inject (0 to 100, integer). Defaults to 100. */ percentage?: number; /** Minimum latency in ms (latency mode) */ min_latency?: number; /** Maximum latency in ms (latency mode) */ max_latency?: number; /** Error message to throw (exception mode) */ exception_msg?: string; /** HTTP status code to return (statuscode mode) */ status_code?: number; /** MB of disk to fill in /tmp (diskspace mode) */ disk_space?: number; /** Array of regex patterns for hosts to block (denylist mode) */ deny_list?: string[]; /** Buffer in ms before Lambda timeout (timeout mode). Default: 0 */ timeout_buffer_ms?: number; /** Replacement body string (corruption mode) */ body?: string; /** Event-based targeting conditions. All conditions must match for the flag to fire. */ match?: MatchCondition[]; } /** The full config: a map of failure mode names to their flag values */ type FailureFlagsConfig = Partial<Record<FailureMode, FlagValue>>; /** A failure resolved and ready to inject */ interface ResolvedFailure { mode: FailureMode; percentage: number; flag: FlagValue; } /** * Generic Lambda handler type. Intentionally broad to support any * event source (API Gateway, SQS, SNS, EventBridge, etc.). */ type LambdaHandler<TEvent = unknown, TResult = unknown> = (event: TEvent, context: Context, callback: Callback<TResult>) => void | Promise<TResult>; /** Options for the injectFailure wrapper */ interface FailureLambdaOptions { /** Override the config source (useful for testing or custom config backends) */ configProvider?: () => Promise<FailureFlagsConfig>; /** Log which failures would fire without actually injecting them */ dryRun?: boolean; } /** Validation error detail */ interface ConfigValidationError { field: string; message: string; value: unknown; } export type { ConfigValidationError as C, FailureFlagsConfig as F, LambdaHandler as L, MatchCondition as M, ResolvedFailure as R, FailureLambdaOptions as a, FailureMode as b, FlagValue as c, MatchOperator as d };