Skip to content

Commit 1e548cd

Browse files
authored
Merge pull request #18 from gunnargrosch/develop
Develop
2 parents 6489ac3 + 81ab22b commit 1e548cd

File tree

4 files changed

+78
-18
lines changed

4 files changed

+78
-18
lines changed

README.md

+38-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
## Description
44

5-
`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.
5+
`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](https://docs.aws.amazon.com/appconfig/latest/userguide/what-is-appconfig.html).
66

7-
## How to install
7+
## How to install with parameter in SSM Parameter Store
88

99
1. Install `failure-lambda` module using NPM.
1010
```bash
@@ -28,11 +28,40 @@ exports.handler = failureLambda(async (event, context) => {
2828
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\"]}"
2929
```
3030
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.
31-
6. Try it out!
31+
6. Add permissions to the parameter for your Lambda function.
32+
7. Try it out!
33+
34+
## How to install with hosted configuration in AWS AppConfig
35+
36+
1. Install `failure-lambda` module using NPM.
37+
```bash
38+
npm install failure-lambda
39+
```
40+
2. Add the module to your Lambda function code.
41+
```js
42+
const failureLambda = require('failure-lambda')
43+
```
44+
3. Wrap your handler.
45+
```js
46+
exports.handler = failureLambda(async (event, context) => {
47+
...
48+
})
49+
```
50+
4. Create Application, Environment, Configuration Profile, and Hosted Configuration in AppConfig console.
51+
5. Deploy a version of the configuration.
52+
6. Add the AWS AppConfig layer for Lambda extensions to your Lambda function. [See details](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-integration-lambda-extensions.html).
53+
7. Add environment variables to your Lambda function.
54+
```bash
55+
FAILURE_APPCONFIG_APPLICATION: YOUR APPCONFIG APPLICATION
56+
FAILURE_APPCONFIG_ENVIRONMENT: YOUR APPCONFIG ENVIRONMENT
57+
FAILURE_APPCONFIG_CONFIGURATION: YOUR APPCONFIG CONFIGURATION PROFILE
58+
```
59+
8. Add permissions to the AppConfig Application, Environment, and Configuration Profile for your Lambda function.
60+
9. Try it out!
3261

3362
## Usage
3463

35-
Edit the values of your parameter in SSM Parameter Store to use the failure injection module.
64+
Edit the values of your parameter in SSM Parameter Store or hosted configuration in AWS AppConfig to use the failure injection module.
3665

3766
* `isEnabled: true` means that failure is injected into your Lambda function.
3867
* `isEnabled: false` means that the failure injection module is disabled and no failure is injected.
@@ -58,6 +87,11 @@ Inspired by Yan Cui's articles on latency injection for AWS Lambda (https://hack
5887

5988
## Changelog
6089

90+
### 2020-10-25 v0.4.0
91+
92+
* 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.
93+
* Hardcoded default configuration with `isEnabled: false`, to use if issues loading configuration from Parameter Store or AppConfig.
94+
6195
### 2020-10-21 v0.3.1
6296

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

lib/failure.js

+31-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
11
'use strict'
22
const aws = require('aws-sdk')
33
const ssm = new aws.SSM()
4+
const fetch = require('node-fetch')
45
const childProcess = require('child_process')
56
const Mitm = require('mitm')
67

7-
async function getConfig() {
8-
try {
9-
let params = {
10-
Name: process.env.FAILURE_INJECTION_PARAM
8+
async function getConfig () {
9+
const defaults = {
10+
isEnabled: false
11+
}
12+
if (process.env.FAILURE_APPCONFIG_CONFIGURATION) {
13+
try {
14+
const url = 'http://localhost:2772/applications/' + process.env.FAILURE_APPCONFIG_APPLICATION + '/environments/' + process.env.FAILURE_APPCONFIG_ENVIRONMENT + '/configurations/' + process.env.FAILURE_APPCONFIG_CONFIGURATION
15+
const response = await fetch(url)
16+
const json = await response.json()
17+
return json
18+
} catch (err) {
19+
console.error(err)
20+
return defaults
21+
}
22+
} else if (process.env.FAILURE_INJECTION_PARAM) {
23+
try {
24+
let params = {
25+
Name: process.env.FAILURE_INJECTION_PARAM
26+
}
27+
let response = await ssm.getParameter(params).promise()
28+
let json = JSON.parse(response.Parameter.Value)
29+
return json
30+
} catch (err) {
31+
console.error(err)
32+
return defaults
1133
}
12-
let request = await ssm.getParameter(params).promise()
13-
return request.Parameter.Value
14-
} catch (err) {
15-
console.error(err)
16-
throw err
34+
} else {
35+
return defaults
1736
}
1837
}
1938
var injectFailure = function (fn) {
2039
return async function () {
2140
try {
22-
let configResponse = await getConfig()
23-
let config = JSON.parse(configResponse)
41+
// let configResponse = await getConfig()
42+
// let config = JSON.parse(configResponse)
43+
let config = await getConfig()
2444
if (config.isEnabled === true && Math.random() < config.rate) {
2545
if (config.failureMode === 'latency') {
2646
let latencyRange = config.maxLatency - config.minLatency

package-lock.json

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "failure-lambda",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"description": "Module for failure injection into AWS Lambda",
55
"main": "./lib/failure.js",
66
"scripts": {
@@ -34,6 +34,7 @@
3434
"eslint-plugin-standard": "^4.0.1"
3535
},
3636
"dependencies": {
37-
"mitm": "^1.7.1"
37+
"mitm": "^1.7.1",
38+
"node-fetch": "^2.6.1"
3839
}
3940
}

0 commit comments

Comments
 (0)