diff --git a/README.md b/README.md index cd99517..362b8da 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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! @@ -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 @@ -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 diff --git a/example/index.js b/example/index.js index c7a71b8..93da08c 100644 --- a/example/index.js +++ b/example/index.js @@ -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({ diff --git a/example/package-lock.json b/example/package-lock.json deleted file mode 100644 index 8311e13..0000000 --- a/example/package-lock.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "requires": true, - "lockfileVersion": 1, - "dependencies": { - "failure-lambda": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/failure-lambda/-/failure-lambda-0.0.1.tgz", - "integrity": "sha512-CzKYywk1xbB3Pg6l2N9HMCgaDpzsgSxLIMWeQkM2C/chhZWZGM8q9UqxM6vvWPyplh79h8DeSUZxfLiJu7/ZJA==" - } - } -} diff --git a/example/serverless.yml b/example/serverless.yml index 7263ac9..c13f238 100644 --- a/example/serverless.yml +++ b/example/serverless.yml @@ -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 diff --git a/lib/failure.js b/lib/failure.js index 5d53c90..2fdfc8c 100644 --- a/lib/failure.js +++ b/lib/failure.js @@ -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 { @@ -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) diff --git a/package.json b/package.json index 142ee6b..a6ac9f8 100644 --- a/package.json +++ b/package.json @@ -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": {