failure-lambda lets you inject faults into AWS Lambda functions to test how they behave under real-world failure conditions.
failure-lambda lets you inject faults into AWS Lambda functions to test how they behave under real-world failure conditions.
failure-lambda lets you inject faults into AWS Lambda functions to test how they behave under real-world failure conditions.
| Path | Runtimes | How | When to use |
|---|---|---|---|
| npm package | Node.js 20+ | Wrap handler or use Middy middleware | Programmatic control, Node.js only |
| Lambda Layer | Any managed runtime | Add layer + 2 env vars | Zero code changes, any runtime |
Both paths support the same failure modes and are controlled via SSM Parameter Store or AppConfig.
Read the blog post for a full walkthrough with examples.
npm install failure-lambdaRequirements: Node.js >= 20 — Lambda runtimes nodejs20.x, nodejs22.x, or nodejs24.x.
import failureLambda from "failure-lambda";
export const handler = failureLambda(async (event, context) => {
// your handler logic
return { statusCode: 200, body: "OK" };
});CommonJS:
const failureLambda = require("failure-lambda");
exports.handler = failureLambda(async (event, context) => {
return { statusCode: 200, body: "OK" };
});…Add to your Lambda function:
FAILURE_INJECTION_PARAM=failureLambdaConfigGrant your Lambda execution role ssm:GetParameter on the parameter.
Enable a failure mode by updating the SSM parameter (or use the CLI):
failure-lambda enable latency --param failureLambdaConfig --region eu-west-1Using AppConfig instead of SSM? See Configuration Sources for setup steps. AppConfig provides deployment strategies and automatic rollback but requires more setup than SSM.
Bundling tip: The only runtime dependency is
@aws-sdk/client-ssm, which the Lambda runtime already provides. If you use esbuild (SAM, CDK), mark@aws-sdk/*as external:External: ["@aws-sdk/*"].
If you use Middy (v4+), use the middleware variant instead of wrapping your handler:
import middy from "@middy/core";
import { failureLambdaMiddleware } from "failure-lambda/middy";
export const handler = middy()
.use(failureLambdaMiddleware())
.handler(async (event, context) => {
return { statusCode: 200, body: "OK" };
});The middleware runs pre-handler failures in its before phase and post-handler failures (corruption) in its after phase. It supports the same configProvider and dryRun options as the wrapper.
The Lambda Layer enables fault injection with zero code changes — no imports, no wrapper, no middleware. Add the layer to your function, set two environment variables, and your existing handler gets chaos engineering capabilities automatically.
Get failure-lambda-layer-x86_64.zip or failure-lambda-layer-aarch64.zip from the latest GitHub release.
aws lambda publish-layer-version \
--layer-name failure-lambda \
--zip-file fileb://failure-lambda-layer-aarch64.zip \
--compatible-architectures arm64 \
--region eu-west-1Add the layer ARN returned by the previous command to your Lambda function's layers.
AWS_LAMBDA_EXEC_WRAPPER=/opt/failure-lambda-wrapper
FAILURE_INJECTION_PARAM=failureLambdaConfig…Grant your Lambda execution role ssm:GetParameter on the parameter.
Enable a failure mode by updating the SSM parameter (or use the CLI):
failure-lambda enable latency --param failureLambdaConfig --region eu-west-1Using AppConfig instead of SSM? See Configuration Sources. Also add the AppConfig Lambda extension layer to your function.
The layer works with all managed Lambda runtimes that support AWS_LAMBDA_EXEC_WRAPPER:
Both x86_64 and arm64 architectures are supported.
…See layer/template.yaml for a full example that builds the layer from source with both x86_64 and arm64 variants.
The layer includes a Rust proxy that sits between the Lambda runtime and the Lambda Runtime API:
/opt/failure-lambda-wrapper) starts the proxy and redirects AWS_LAMBDA_RUNTIME_API to itdenylist mode, an LD_PRELOAD shared library intercepts getaddrinfo() calls to block DNS resolution for matching hostnamesYour handler code is completely unchanged — the proxy is transparent.
AWS_LAMBDA_EXEC_WRAPPER, which is silently ignored on OS-only runtimes (provided.al2023, provided.al2).getaddrinfo(), which does not work with runtimes that use statically linked DNS. The Node.js npm package uses dns.lookup monkey-patching instead, which is more reliable for Node.js. All other failure modes work regardless of runtime.FAILURE_LAMBDA_DISABLED is not implemented in the layer proxy. To disable injection, set all flags to enabled: false in the configuration.To build the layer from source instead of downloading, see layer/build.sh.
| Mode | Description |
|---|---|
latency |
Adds random delay between min_latency and max_latency ms |
timeout |
Sleeps until Lambda timeout minus a configurable buffer |
exception |
Throws an error with a configurable message |
statuscode |
Returns a response with a configurable HTTP status code, skipping the handler |
diskspace |
Fills /tmp with a configurable amount of data |
denylist |
Blocks outgoing network connections to hostnames matching regex patterns |
corruption |
Replaces or mangles the handler's response body (post-handler) |
Multiple modes can be active simultaneously. Each mode is an independent feature flag with its own percentage (probability of injection).
To enable a single mode with minimal config:
{"latency": {"enabled": true, "min_latency": 200, "max_latency": 500}}When multiple modes are enabled, pre-handler failures run first, then the handler executes, then post-handler failures modify the response:
Pre-handler (before the handler):
latency — adds delay, then continuestimeout — sleeps until Lambda timeout minus buffer, then continuesdiskspace — fills /tmp, then continuesdenylist — blocks matching network hosts, then continuesstatuscode — returns status code response, skips handlerexception — throws error, skips handlerPost-handler (after the handler returns):
7. corruption — corrupts or replaces the handler's response
Each flag's percentage is rolled independently.
Each failure mode is an independent feature flag. This format is used by both SSM Parameter Store and AppConfig.
…When a flag is disabled, only {"enabled": false} is needed — attributes are optional. When enabled, percentage defaults to 100 if omitted.
| Flag | Attribute | Type | Description |
|---|---|---|---|
| all | enabled |
boolean |
Enable/disable this failure mode |
| all | percentage |
integer |
Percentage of invocations to inject (0–100). Default: 100 |
| all | match |
object[] |
Event-based targeting conditions (see below) |
latency |
min_latency |
number |
Minimum latency in ms |
latency |
max_latency |
number |
Maximum latency in ms |
exception |
exception_msg |
string |
Error message thrown |
statuscode |
status_code |
number |
HTTP status code returned (100-599) |
diskspace |
disk_space |
number |
MB of disk to fill in /tmp (1–10240) |
denylist |
deny_list |
string[] |
Regex patterns; matching hosts are blocked. Patterns with nested quantifiers are rejected to prevent ReDoS. |
timeout |
timeout_buffer_ms |
number |
Buffer in ms before Lambda timeout. Default: 0 |
corruption |
body |
string |
Replacement response body. If omitted, body is mangled. |
Use match conditions to restrict injection to specific requests — e.g. only affect production traffic or specific API routes. Each condition specifies a dot-separated path into the event. All conditions must match for the flag to fire.
{
"corruption": {
"enabled": true,
"percentage": 30,
"body": "{\"error\": \"corrupted\"}",
"match": [
{ "path": "requestContext.http.method", "value": "GET" },
{ "path": "requestContext.stage", "value": "prod" }
]
}
}This example only corrupts GET requests to the prod stage. Flags without match apply to all invocations.
Each condition supports an optional operator field (defaults to "eq"):
| Operator | Description | value required? |
|---|---|---|
eq |
Exact string equality (default) | Yes |
exists |
Path exists and is not null/undefined (falsy values like 0, "", false are considered to exist) |
No |
startsWith |
Value starts with the given prefix | Yes |
regex |
Value matches the regular expression | Yes |
{
"latency": {
"enabled": true,
"percentage": 100,
"min_latency": 200,
"max_latency": 500,
"match": [
{ "path": "requestContext.http.path", "operator": "startsWith", "value": "/api" },
{ "path": "headers.x-debug", "operator": "exists" },
{ "path": "requestContext.http.method", "operator": "regex", "value": "^(GET|HEAD)$" }
]
}
}Header casing: API Gateway lowercases all header keys in the Lambda event (e.g.
X-Chaos-Enabledbecomesx-chaos-enabled). Always use lowercase header names in match paths:headers.x-debug, notheaders.X-Debug.
Configuration is cached in memory to reduce latency and API calls. The cache persists within a single Lambda container and resets on cold starts.
FAILURE_CACHE_TTL). The parameter name must match FAILURE_INJECTION_PARAM.No open issues yet, or sync has not completed.