Home

Awesome

Failure injection for AWS Lambda - failure-lambda

Description

failure-lambda is a small Node module for injecting failure into AWS Lambda (https://aws.amazon.com/lambda). It offers a simple failure injection wrapper for your Lambda handler where you then can choose to inject failure by setting the failureMode to latency, exception, denylist, diskspace or statuscode. You control your failure injection using SSM Parameter Store or AWS AppConfig.

How to install with parameter in SSM Parameter Store

  1. Install failure-lambda module using NPM.
npm install failure-lambda
  1. Add the module to your Lambda function code.
const failureLambda = require('failure-lambda')
  1. Wrap your handler.
exports.handler = failureLambda(async (event, context) => {
  ...
})
  1. Create a parameter in SSM Parameter Store.
{"isEnabled": false, "failureMode": "latency", "rate": 1, "minLatency": 100, "maxLatency": 400, "exceptionMsg": "Exception message!", "statusCode": 404, "diskSpace": 100, "denylist": ["s3.*.amazonaws.com", "dynamodb.*.amazonaws.com"]}
aws ssm put-parameter --region eu-west-1 --name failureLambdaConfig --type String --overwrite --value "{\"isEnabled\": false, \"failureMode\": \"latency\", \"rate\": 1, \"minLatency\": 100, \"maxLatency\": 400, \"exceptionMsg\": \"Exception message!\", \"statusCode\": 404, \"diskSpace\": 100, \"denylist\": [\"s3.*.amazonaws.com\", \"dynamodb.*.amazonaws.com\"]}"
  1. Add an environment variable to your Lambda function with the key FAILURE_INJECTION_PARAM and the value set to the name of your parameter in SSM Parameter Store.
  2. Add permissions to the parameter for your Lambda function.
  3. Try it out!

How to install with hosted configuration in AWS AppConfig

  1. Install failure-lambda module using NPM.
npm install failure-lambda
  1. Add the module to your Lambda function code.
const failureLambda = require('failure-lambda')
  1. Wrap your handler.
exports.handler = failureLambda(async (event, context) => {
  ...
})
  1. Create Application, Environment, Configuration Profile, and Hosted Configuration in AppConfig console.
  2. Deploy a version of the configuration.
  3. Add the AWS AppConfig layer for Lambda extensions to your Lambda function. See details.
  4. Add environment variables to your Lambda function.
FAILURE_APPCONFIG_APPLICATION: YOUR APPCONFIG APPLICATION
FAILURE_APPCONFIG_ENVIRONMENT: YOUR APPCONFIG ENVIRONMENT
FAILURE_APPCONFIG_CONFIGURATION: YOUR APPCONFIG CONFIGURATION PROFILE
  1. Add permissions to the AppConfig Application, Environment, and Configuration Profile for your Lambda function.
  2. Try it out!

Usage

Edit the values of your parameter in SSM Parameter Store or hosted configuration in AWS AppConfig to use the failure injection module.

Example

In the subfolder example is a sample application which will install an AWS Lambda function, an Amazon DynamoDB table, and a parameter in SSM Parameter Store. You can install it using AWS SAM, AWS CDK, or Serverless Framework.

AWS SAM

cd example/sam
npm install
sam build
sam deploy --guided

AWS CDK

cd example/cdk
npm install
cdk deploy

Serverless Framework

cd example/sls
npm install
sls deploy

Notes

Inspired by Yan Cui's articles on latency injection for AWS Lambda (https://hackernoon.com/chaos-engineering-and-aws-lambda-latency-injection-ddeb4ff8d983) and Adrian Hornsby's chaos injection library for Python (https://github.com/adhorn/aws-lambda-chaos-injection/).

Changelog

2022-02-14 v0.4.4

2022-02-14 v0.4.3

2021-03-16 v0.4.2

2020-10-26 v0.4.1

2020-10-25 v0.4.0

2020-10-21 v0.3.1

2020-08-24 v0.3.0

2020-02-17 v0.2.0

2020-02-13 v0.1.1

2019-12-30 v0.1.0

2019-12-23 v0.0.1

Contributors

Gunnar Grosch - GitHub | Twitter | LinkedIn

Jason Barto - GitHub | Twitter | LinkedIn

License

This code is made available under the MIT-0 license. See the LICENSE file.