Skip to content

Commit

Permalink
Merge pull request #11 from gunnargrosch/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
gunnargrosch authored Aug 24, 2020
2 parents 3c90b7c + 53b2ee8 commit 71b947e
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 492 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 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`, `blacklist`, `diskspace` or `statuscode`. You control your failure injection using SSM Parameter Store.
`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.

## How to install

Expand All @@ -22,10 +22,10 @@ exports.handler = failureLambda(async (event, context) => {
```
4. Create a parameter in SSM Parameter Store.
```json
{"isEnabled": false, "failureMode": "latency", "rate": 1, "minLatency": 100, "maxLatency": 400, "exceptionMsg": "Exception message!", "statusCode": 404, "diskSpace": 100, "blacklist": ["s3.*.amazonaws.com", "dynamodb.*.amazonaws.com"]}
{"isEnabled": false, "failureMode": "latency", "rate": 1, "minLatency": 100, "maxLatency": 400, "exceptionMsg": "Exception message!", "statusCode": 404, "diskSpace": 100, "denylist": ["s3.*.amazonaws.com", "dynamodb.*.amazonaws.com"]}
```
```bash
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, \"blacklist\": [\"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\"]}"
```
5. 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.
6. Try it out!
Expand All @@ -36,13 +36,13 @@ Edit the values of your parameter in SSM Parameter Store to use the failure inje

* `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`, `blacklist`, `diskspace` or `statuscode` as explained below.
* `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`.
* `blacklist` is an array of regular expressions, if a connection is made to a host matching one of the regular expressions it will be blocked.
* `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

Expand All @@ -58,9 +58,15 @@ Inspired by Yan Cui's articles on latency injection for AWS Lambda (https://hack

## Changelog

### 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 blacklist failure.
* Added block list failure.
* Updated example application to store file in S3 and item in DynamoDB.

### 2020-02-13 v0.1.1
Expand Down
8 changes: 8 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "",
"version": "0.0.0",
"description": "",
"dependencies": {
"failure-lambda": ""
}
}
2 changes: 1 addition & 1 deletion example/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ resources:
Type: 'AWS::SSM::Parameter'
Properties:
Type: String
Value: '{"isEnabled": false, "failureMode": "latency", "rate": 1, "minLatency": 100, "maxLatency": 400, "exceptionMsg": "Exception message!", "statusCode": 404, "diskSpace": 100, "blacklist": ["s3.*.amazonaws.com", "dynamodb.*.amazonaws.com"]}'
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"]}'
failureLambdaBucket:
Type: 'AWS::S3::Bucket'
Properties:
Expand Down
8 changes: 4 additions & 4 deletions lib/failure.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ var injectFailure = function (fn) {
} else if (config.failureMode === 'diskspace') {
console.log('Injecting disk space: ' + config.diskSpace + ' MB')
childProcess.spawnSync('dd', ['if=/dev/zero', 'of=/tmp/diskspace-failure-' + Date.now() + '.tmp', 'count=1000', 'bs=' + config.diskSpace * 1000])
} else if (config.failureMode === 'blacklist') {
console.log('Injecting dependency failure through a network blackhole for blacklisted sites: ' + config.blacklist)
} else if (config.failureMode === 'denylist') {
console.log('Injecting dependency failure through a network block for denylisted sites: ' + config.denylist)
let mitm = Mitm()
let blRegexs = []
config.blacklist.forEach(function (regexStr) {
config.denylist.forEach(function (regexStr) {
blRegexs.push(new RegExp(regexStr))
})
mitm.on('connect', function (socket, opts) {
mitm.on('connection', function (socket, opts) {
let block = false
blRegexs.forEach(function (blRegex) {
if (blRegex.test(opts.host)) {
Expand Down
Loading

0 comments on commit 71b947e

Please sign in to comment.