Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
F

failure-lambda

> 编程语言
Open source

failure-lambda lets you inject faults into AWS Lambda functions to test how they behave under real-world failure conditions.

100 stars0 likes1 views
WebsiteGitHub

About

failure-lambda lets you inject faults into AWS Lambda functions to test how they behave under real-world failure conditions.

failure-lambda

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.

Table of Contents

  • Getting Started: npm Package
  • Getting Started: Lambda Layer
  • Failure Modes
  • Configuration
  • Configuration Sources
  • Environment Variables
  • CLI
  • Logging
  • Advanced Usage
  • Examples
  • Migration from 0.x
  • Contributing
  • Acknowledgments
  • License

Getting Started: npm Package

1. Install

bash
npm install failure-lambda

Requirements: Node.js >= 20 — Lambda runtimes nodejs20.x, nodejs22.x, or nodejs24.x.

2. Wrap your handler

typescript
import failureLambda from "failure-lambda";

export const handler = failureLambda(async (event, context) => {
  // your handler logic
  return { statusCode: 200, body: "OK" };
});

CommonJS:

javascript
const failureLambda = require("failure-lambda");

exports.handler = failureLambda(async (event, context) => {
  return { statusCode: 200, body: "OK" };
});

3. Create the SSM parameter

…

4. Set the environment variable

Add to your Lambda function:

bash
FAILURE_INJECTION_PARAM=failureLambdaConfig

5. Add IAM permission

Grant your Lambda execution role ssm:GetParameter on the parameter.

6. Deploy and test

Enable a failure mode by updating the SSM parameter (or use the CLI):

bash
failure-lambda enable latency --param failureLambdaConfig --region eu-west-1

Using 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/*"].

Using Middy?

If you use Middy (v4+), use the middleware variant instead of wrapping your handler:

typescript
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.

Getting Started: Lambda Layer

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.

1. Download the layer zip

Get failure-lambda-layer-x86_64.zip or failure-lambda-layer-aarch64.zip from the latest GitHub release.

2. Publish the layer to your account

bash
aws lambda publish-layer-version \
  --layer-name failure-lambda \
  --zip-file fileb://failure-lambda-layer-aarch64.zip \
  --compatible-architectures arm64 \
  --region eu-west-1

3. Add the layer to your function

Add the layer ARN returned by the previous command to your Lambda function's layers.

4. Set the environment variables

bash
AWS_LAMBDA_EXEC_WRAPPER=/opt/failure-lambda-wrapper
FAILURE_INJECTION_PARAM=failureLambdaConfig

5. Create the SSM parameter

…

6. Add IAM permission

Grant your Lambda execution role ssm:GetParameter on the parameter.

7. Deploy and test

Enable a failure mode by updating the SSM parameter (or use the CLI):

bash
failure-lambda enable latency --param failureLambdaConfig --region eu-west-1

Using AppConfig instead of SSM? See Configuration Sources. Also add the AppConfig Lambda extension layer to your function.

Supported Runtimes

The layer works with all managed Lambda runtimes that support AWS_LAMBDA_EXEC_WRAPPER:

  • Node.js: nodejs24.x, nodejs22.x, nodejs20.x
  • Python: python3.14, python3.13, python3.12, python3.11, python3.10
  • Java: java25, java21, java17, java11, java8.al2
  • .NET: dotnet10, dotnet8
  • Ruby: ruby3.4, ruby3.3, ruby3.2

Both x86_64 and arm64 architectures are supported.

SAM Example

…

See layer/template.yaml for a full example that builds the layer from source with both x86_64 and arm64 variants.

How It Works

The layer includes a Rust proxy that sits between the Lambda runtime and the Lambda Runtime API:

  1. The wrapper script (/opt/failure-lambda-wrapper) starts the proxy and redirects AWS_LAMBDA_RUNTIME_API to it
  2. On each invocation, the proxy reads your failure configuration from SSM Parameter Store or AppConfig
  3. Based on the active flags, the proxy injects faults before or after forwarding the invocation to your handler
  4. For denylist mode, an LD_PRELOAD shared library intercepts getaddrinfo() calls to block DNS resolution for matching hostnames

Your handler code is completely unchanged — the proxy is transparent.

Limitations

  • Managed runtimes only: Relies on AWS_LAMBDA_EXEC_WRAPPER, which is silently ignored on OS-only runtimes (provided.al2023, provided.al2).
  • DNS denylist: Uses LD_PRELOAD on 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.
  • No kill switch: 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.

Failure Modes

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:

json
{"latency": {"enabled": true, "min_latency": 200, "max_latency": 500}}

Injection Order

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):

  1. latency — adds delay, then continues
  2. timeout — sleeps until Lambda timeout minus buffer, then continues
  3. diskspace — fills /tmp, then continues
  4. denylist — blocks matching network hosts, then continues
  5. statuscode — returns status code response, skips handler
  6. exception — throws error, skips handler

Post-handler (after the handler returns): 7. corruption — corrupts or replaces the handler's response

Each flag's percentage is rolled independently.

Configuration

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 Attributes

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.

Event-Based Targeting

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.

json
{
  "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.

Match Operators

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
json
{
  "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-Enabled becomes x-chaos-enabled). Always use lowercase header names in match paths: headers.x-debug, not headers.X-Debug.

Configuration Sources

Configuration is cached in memory to reduce latency and API calls. The cache persists within a single Lambda container and resets on cold starts.

  • SSM Parameter Store: Defaults to a 60-second cache TTL (configurable via FAILURE_CACHE_TTL). The parameter name must match FAILURE_INJECTION_PARAM.
  • AppConfig: Cache is auto-disabled (TTL defaults to 0) because the AppConfig Lambda extension already handles caching at its own poll interva

Issues· 13 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptawschaoschaosengineeringfailure

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言