Skip to content

Commit

Permalink
Merge pull request #2 from gunnargrosch/develop
Browse files Browse the repository at this point in the history
Added disk space failure in module and example.
  • Loading branch information
gunnargrosch authored Dec 30, 2019
2 parents bafcaf4 + 891d22e commit 5343ff5
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
12 changes: 9 additions & 3 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` 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`, `statuscode` or `diskspace`. 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}
{"isEnabled": false, "failureMode": "latency", "rate": 1, "minLatency": 100, "maxLatency": 400, "exceptionMsg": "Exception message!", "statusCode": 404, "diskSpace": 100}
```
```bash
aws ssm put-parameter --region eu-north-1 --name failureLambdaConfig --type String --overwrite --value "{\"isEnabled\": false, \"failureMode\": \"latency\", \"rate\": 1, \"minLatency\": 100, \"maxLatency\": 400, \"exceptionMsg\": \"Exception message!\", \"statusCode\": 404}"
aws ssm put-parameter --region eu-north-1 --name failureLambdaConfig --type String --overwrite --value "{\"isEnabled\": false, \"failureMode\": \"latency\", \"rate\": 1, \"minLatency\": 100, \"maxLatency\": 400, \"exceptionMsg\": \"Exception message!\", \"statusCode\": 404, \"diskSpace\": 100}"
```
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 @@ -41,6 +41,7 @@ Edit the values of your parameter in SSM Parameter Store to use the failure inje
* `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`.

## Example

Expand All @@ -56,6 +57,11 @@ Inspired by Yan Cui's articles on latency injection for AWS Lambda (https://hack

## Changelog

### 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
Expand Down
4 changes: 4 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
'use strict'
const failureLambda = require('failure-lambda')
const fs = require('fs')
let response

exports.handler = failureLambda(async (event, context) => {
try {
fs.writeFile('/tmp/example-' + Date.now() + '.tmp', 'Contents', (err) => {
if (err) throw err
})
response = {
statusCode: 200,
body: JSON.stringify({
Expand Down
11 changes: 0 additions & 11 deletions example/package-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion example/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ resources:
Properties:
Name: ${self:service}-${opt:stage, self:provider.stage}-failureLambdaExample
Type: String
Value: '{"isEnabled": false, "failureMode": "latency", "rate": 1, "minLatency": 100, "maxLatency": 400, "exceptionMsg": "Exception message!", "statusCode": 404}'
Value: '{"isEnabled": false, "failureMode": "latency", "rate": 1, "minLatency": 100, "maxLatency": 400, "exceptionMsg": "Exception message!", "statusCode": 404, "diskSpace": 100}'
package:
exclude:
- .vscode
Expand Down
4 changes: 4 additions & 0 deletions lib/failure.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
const aws = require('aws-sdk')
const ssm = new aws.SSM()
const childProcess = require('child_process')

async function getConfig () {
try {
Expand Down Expand Up @@ -32,6 +33,9 @@ var injectFailure = function (fn) {
console.log('Injecting status code: ' + config.statusCode)
let response = { statusCode: config.statusCode }
return response
} 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])
}
}
return fn.apply(this, arguments)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "failure-lambda",
"version": "0.0.1",
"version": "0.1.0",
"description": "Module for failure injection into AWS Lambda",
"main": "./lib/failure.js",
"scripts": {
Expand Down

0 comments on commit 5343ff5

Please sign in to comment.