failure-cloudfunctions is a small Node module for injecting failure into Google Cloud Functions (https://cloud.google.com/functions/). It offers a simple failure injection wrapper for your Cloud Functions 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 a secret in Secret Manager.
- Install 
failure-cloudfunctionsmodule using NPM. 
npm install failure-cloudfunctions- Add the module to your Cloud Functions function code.
 
const failureCloudFunctions = require('failure-cloudfunctions')- Wrap your handler.
 
exports.handler = failureCloudFunctions(async (req, res) => {
  ...
})- Create a secret in Secret Manager.
 
{"isEnabled": false, "failureMode": "latency", "rate": 1, "minLatency": 100, "maxLatency": 400, "exceptionMsg": "Exception message!", "statusCode": 404, "diskSpace": 100, "denylist": ["storage.googleapis.com"]}gcloud beta secrets create <your-secret-name> --replication-policy="automatic"
echo -n "{\"isEnabled\": false, \"failureMode\": \"latency\", \"rate\": 1, \"minLatency\": 100, \"maxLatency\": 400, \"exceptionMsg\": \"Exception message!\", \"statusCode\": 404, \"diskSpace\": 100, \"denylist\": [\"storage.googleapis.com\"]}" | gcloud beta secrets versions add <your-secret-name> --data-file=-- Add environment variables to your Cloud Function with values from above.
 
GCP_PROJECT=<your-gcp-project-id>
FAILURE_INJECTION_PARAM=<your-secret-name>- Give your Cloud Function access to your secret in Secret Manager.
 
gcloud beta secrets add-iam-policy-binding <your-secret-name> --role roles/secretmanager.secretAccessor --member serviceAccount:<your-gcp-project-id>@appspot.gserviceaccount.com- Try it out!
 
Edit the values of your parameter in Secret Manager to use the failure injection module.
isEnabled: truemeans that failure is injected into your Cloud Function.isEnabled: falsemeans that the failure injection module is disabled and no failure is injected.failureModeselects which failure you want to inject. The options arelatency,exception,denylist,diskspaceorstatuscodeas explained below.ratecontrols 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.minLatencyandmaxLatencyis the span of latency in milliseconds injected into your function whenfailureModeis set tolatency.exceptionMsgis the message thrown with the exception created whenfailureModeis set toexception.statusCodeis the status code returned by your function whenfailureModeis set tostatuscode.diskSpaceis size in MB of the file created in tmp whenfailureModeis set todiskspace.denylistis an array of regular expressions, if a connection is made to a host matching one of the regular expressions it will be blocked.
In the subfolder example is a simple function which can be installed in Google Cloud and used for test.
Inspired by Yan Cui's articles on latency injection for Google Cloud Functions (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/).
- 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.
 
- Added permission info to documentation.
 - Fixed tmp path in library and example.
 
- Fixed Secret Manager integration.
 - Added simple example.
 - Updated documentation.
 
- Initial release