Giter Club home page Giter Club logo

failure-lambda's Introduction

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.

  • isEnabled: true means that failure is injected into your Lambda function.
  • isEnabled: false means that the failure injection module is disabled and no failure is injected.
  • failureMode selects which failure you want to inject. The options are latency, exception, denylist, diskspace or statuscode as explained below.
  • rate controls the rate of failure. 1 means that failure is injected on all invocations and 0.5 that failure is injected on about half of all invocations.
  • minLatency and maxLatency is the span of latency in milliseconds injected into your function when failureMode is set to latency.
  • exceptionMsg is the message thrown with the exception created when failureMode is set to exception.
  • statusCode is the status code returned by your function when failureMode is set to statuscode.
  • diskSpace is size in MB of the file created in tmp when failureMode is set to diskspace.
  • denylist is an array of regular expressions, if a connection is made to a host matching one of the regular expressions it will be blocked.

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

  • Switch to node-fetch@2.

2022-02-14 v0.4.3

  • Updated dependencies.

2021-03-16 v0.4.2

  • Puts the mitm object in the library global namespace so that it persists across function invocations.
  • Syntax formatting.

2020-10-26 v0.4.1

  • Made AppConfig Lambda extension port configurable using environment variable.

2020-10-25 v0.4.0

  • Added optional support for AWS AppConfig, allowing to validate failure configuration, deploy configuration using gradual or non-gradual deploy strategy, monitor deployed configuration with automatical rollback if CloudWatch Alarms is configured, and caching of configuration.
  • Hardcoded default configuration with isEnabled: false, to use if issues loading configuration from Parameter Store or AppConfig.

2020-10-21 v0.3.1

  • Change mitm mode back to connect to fix issue with all connections being blocked.

2020-08-24 v0.3.0

  • Changed mitm mode from connect to connection for quicker enable/disable of failure injection.
  • Renamed block list failure injection to denylist (breaking change for that failure mode).
  • Updated dependencies.

2020-02-17 v0.2.0

  • Added block list failure.
  • Updated example application to store file in S3 and item in DynamoDB.

2020-02-13 v0.1.1

  • Fixed issue with exception injection not throwing the exception.

2019-12-30 v0.1.0

  • Added disk space failure.
  • Updated example application to store example file in tmp.

2019-12-23 v0.0.1

  • Initial release

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.

failure-lambda's People

Contributors

gunnargrosch avatar jpbarto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

failure-lambda's Issues

Feature Request: deny access to service dependencies

It would be great to be able to simulate failure of a service dependency (access to DynamoDB for example). This could be simulated through manipulation of IAM roles / permissions but it would be great to be able to extend this to other network dependencies (MongoDB connectivity, other microservices, etc).

Would it make sense to configure failure-lambda to simulate the failure of a network dependency through interception of the network calls?

Exception returns successfully

When setting 'failureMode' to 'exception' the Lambda function returns successfully, an exception occurs but the Lambda function succeeds.

START RequestId: 303ca17f-a63c-44ce-a38e-9236e3db57c2 Version: $LATEST
2020-02-07T14:18:14.839Z	303ca17f-a63c-44ce-a38e-9236e3db57c2	INFO	Injecting exception message: Exception message!
2020-02-07T14:18:14.852Z	303ca17f-a63c-44ce-a38e-9236e3db57c2	INFO	Error: Exception message!
    at Runtime.handler (/var/task/node_modules/failure-lambda/lib/failure.js:31:17)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)END RequestId: 303ca17f-a63c-44ce-a38e-9236e3db57c2
REPORT RequestId: 303ca17f-a63c-44ce-a38e-9236e3db57c2	Duration: 142.89 ms	Billed Duration: 200 ms	Memory Size: 128 MB	Max Memory Used: 96 MB	

I would have expected a thrown exception to cause the Lambda function to fail.

mitm.bypass() stops working after changing the event name in the handler.

I would like to ask why the event was changed to the connection in this commit ae590c8f7b73b1eec64ab2460b0987df03d4cddc ?

The current change is quite crucial, while it breaks the logic of usingsocket.bypass(). When we start relying on the connection event, then it's not possible to bypass the mitm hook and continue the origin request, because the check of bypassed flag in mitm is done on the connect event.
mitm.connect

I am looking forward to your response.

Unable to deploy a lambda on AWS after including failure-lambda library

We are getting following error message while trying to deploy a lambda via CF template on AWS after including failure-lambda library.

UPDATE_FAILED: Resource handler returned message: "Unzipped size must be smaller than 262144000 bytes (Service: Lambda, Status Code: 400)

Can you please help?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.