-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathchaos_lambda.py
359 lines (284 loc) · 10.6 KB
/
chaos_lambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# -*- coding: utf-8 -*-
"""
Chaos Injection for AWS Lambda - chaos_lambda
======================================================
|docs| |issues| |Maintenance| |Pypi| |Travis| |Coveralls| |twitter|
.. |docs| image:: https://readthedocs.org/projects/aws-lambda-chaos-injection/badge/?version=latest
:target: https://aws-lambda-chaos-injection.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status
.. |twitter| image:: https://img.shields.io/twitter/url/https/github.com/adhorn/aws-lambda-chaos-injection?style=social
:alt: Twitter
:target: https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fgithub.com%2Fadhorn%2Faws-lambda-chaos-injection
.. |issues| image:: https://img.shields.io/github/issues/adhorn/aws-lambda-chaos-injection
:alt: Issues
.. |Maintenance| image:: https://img.shields.io/badge/Maintained%3F-yes-green.svg
:alt: Maintenance
:target: https://GitHub.com/adhorn/aws-lambda-chaos-injection/graphs/commit-activity
.. |Pypi| image:: https://badge.fury.io/py/chaos-lambda.svg
:target: https://badge.fury.io/py/chaos-lambda
.. |Travis| image:: https://api.travis-ci.org/adhorn/aws-lambda-chaos-injection.svg?branch=master
:target: https://travis-ci.org/adhorn/aws-lambda-chaos-injection
.. |Coveralls| image:: https://coveralls.io/repos/github/adhorn/aws-lambda-chaos-injection/badge.svg?branch=master
:target: https://coveralls.io/github/adhorn/aws-lambda-chaos-injection?branch=master
``chaos_lambda`` is a small library injecting chaos into `AWS Lambda
<https://aws.amazon.com/lambda/>`_.
It offers simple python decorators to do `delay`, `exception` and `statusCode` injection for your AWS Lambda function.
This allows to conduct small chaos engineering experiments for your serverless application
in the `AWS Cloud <https://aws.amazon.com>`_.
* Support for Latency injection using ``fault_type: latency``
* Support for Exception injection using ``fault_type: exception``
* Support for HTTP Error status code injection using ``fault_type: status_code``
* Using for SSM Parameter Store to control the experiment using ``is_enabled: true``
* Support for adding rate of failure using ``rate``. (Default rate = 1)
* Per Lambda function injection control using Environment variable (``CHAOS_PARAM``)
Install
--------
.. code:: shell
pip install chaos-lambda
Example
--------
.. code:: python
# function.py
import os
from chaos_lambda import inject_fault
# this should be set as a Lambda environment variable
os.environ['CHAOS_PARAM'] = 'chaoslambda.config'
@inject_fault
def handler(event, context):
return {
'statusCode': 200,
'body': 'Hello from Lambda!'
}
Considering a configuration as follows:
.. code:: json
{
"fault_type": "exception",
"delay": 400,
"is_enabled": true,
"error_code": 404,
"exception_msg": "This is chaos",
"rate": 1
}
When excecuted, the Lambda function, e.g ``handler('foo', 'bar')``, will produce the following result:
.. code:: shell
exception_msg from config chaos with a rate of 1
corrupting now
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../chaos_lambda.py", line 199, in wrapper
raise Exception(exception_msg)
Exception: This is chaos
Configuration
-------------
The configuration for the failure injection is stored in the `AWS SSM Parameter Store
<https://aws.amazon.com/ssm/>`_ and accessed at runtime by the ``get_config()``
function:
.. code:: json
{
"fault_type": "exception",
"delay": 400,
"is_enabled": true,
"error_code": 404,
"exception_msg": "This is chaos",
"rate": 1
}
To store the above configuration into SSM using the `AWS CLI <https://aws.amazon.com/cli>`_ do the following:
.. code:: shell
aws ssm put-parameter --name chaoslambda.config --type String --overwrite --value "{ \"delay\": 400, \"is_enabled\": true, \"error_code\": 404, \"exception_msg\": \"This is chaos\", \"rate\": 1, \"fault_type\": \"exception\"}" --region eu-west-1
AWS Lambda will need to have `IAM access to SSM <https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-paramstore-access.html>`_.
.. code:: json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssm:DescribeParameters"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"ssm:GetParameters",
"ssm:GetParameter"
],
"Resource": "arn:aws:ssm:eu-north-1:12345678910:parameter/chaoslambda.config"
}
]
}
Supported Faults:
---------------------
``chaos_lambda`` currently supports the following faults:
* `latency` - Add latency in the AWS Lambda execution
* `exception` - Raise an exception during the AWS Lambda execution
* `status_code` - force AWS Lambda to return a specific HTTP error code
More information:
-----------------
"""
from __future__ import division, unicode_literals
from functools import wraps
import os
import time
import logging
import random
import json
from ssm_cache import SSMParameter
from ssm_cache.cache import InvalidParameterError
logger = logging.getLogger(__name__)
__version__ = '0.3'
def get_config():
"""
Retrieve the full configuration from the SSM parameter store
The config returns a dictionary
value: requested configuration
How to use::
>>> import os
>>> from chaos_lambda import get_config
>>> os.environ['CHAOS_PARAM'] = 'chaoslambda.config'
>>> get_config()
{'delay': 500, 'is_enabled': True, 'error_code': 404, 'exception_msg': 'chaos', 'rate': 1, 'fault_type': 'latency'}
"""
param = SSMParameter(os.environ['CHAOS_PARAM'])
try:
value = json.loads(param.value)
if not value["is_enabled"]:
return
return value
except InvalidParameterError as ex:
# key does not exist in SSM
raise InvalidParameterError("{} is not a valid SSM config".format(ex))
def inject_fault(func):
"""
Add failure to the lambda function based on the value of 'fault_type' present
in the config returned from the SSM parameter store
Given SSM Configuration::
{
"fault_type": "latency",
"delay": 400,
"is_enabled": true,
"error_code": 404,
"exception_msg": "chaos",
"rate": 1
}
Usage::
>>> @inject_fault
... def handler(event, context):
... return {
... 'statusCode': 200,
... 'body': 'Hello from Lambda!'
... }
>>> handler('foo', 'bar')
Injecting 400 of delay with a rate of 1
Added 402.20ms to handler
{'statusCode': 200, 'body': 'Hello from Lambda!'}
Given SSM Configuration::
{
"fault_type": "exception",
"delay": 400,
"is_enabled": true,
"error_code": 404,
"exception_msg": "chaos",
"rate": 1
}
Usage::
>>> @inject_fault
... def handler(event, context):
... return {
... 'statusCode': 200,
... 'body': 'Hello from Lambda!'
... }
>>> handler('foo', 'bar')
Injecting exception_type <class "Exception"> with message chaos a rate of 1
corrupting now
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../chaos_lambda.py", line 316, in wrapper
raise _exception_type(_exception_msg)
Exception: chaos
Given SSM Configuration::
{
"fault_type": "status_code",
"delay": 400,
"is_enabled": true,
"error_code": 404,
"exception_msg": "chaos",
"rate": 1
}
Usage::
>>> @inject_fault
... def handler(event, context):
... return {
... 'statusCode': 200,
... 'body': 'Hello from Lambda!'
... }
>>> handler('foo', 'bar')
Injecting Error 404 at a rate of 1
corrupting now
{'statusCode': 404, 'body': 'Hello from Lambda!'}
"""
@wraps(func)
def wrapper(*args, **kwargs):
_chaos_conf = get_config()
if not _chaos_conf:
return func(*args, **kwargs)
logger.info(
"Got SSM configuration: %s",
_chaos_conf
)
_fault_type = _chaos_conf.get('fault_type')
rate = _chaos_conf.get("rate")
if _fault_type == "latency":
if isinstance(_chaos_conf.get("delay"), int):
_delay = _chaos_conf.get("delay")
else:
logger.info("Parameter delay is no valid int")
return func(*args, **kwargs)
logger.info(
"Injecting %d ms of delay with a rate of %s",
_delay, rate
)
start = time.time()
if _delay > 0 and rate >= 0:
# add latency approx rate% of the time
if round(random.random(), 5) <= rate:
logger.debug('sleeping now')
time.sleep(_delay / 1000.0)
end = time.time()
logger.debug(
'Added %.2fms to %s',
(end - start) * 1000,
func.__name__
)
if _fault_type == "exception":
_exception_type = Exception
if isinstance(_chaos_conf.get("exception_msg"), str):
_exception_msg = _chaos_conf.get("exception_msg")
else:
logger.info("Parameter exception_msg is no valid string")
return func(*args, **kwargs)
logger.info(
"Injecting exception_type %s with message %s a rate of %d",
_exception_type,
_exception_msg,
rate
)
# add injection approx rate% of the time
if round(random.random(), 5) <= rate:
logger.debug("corrupting now")
raise _exception_type(_exception_msg)
if _fault_type == "status_code":
result = func(*args, **kwargs)
if isinstance(_chaos_conf.get("error_code"), int):
_error_code = _chaos_conf.get("error_code")
else:
logger.info("Parameter error_code is no valid int")
return func(*args, **kwargs)
logger.info("Injecting Error %s at a rate of %d", _error_code, rate)
# add injection approx rate% of the time
if round(random.random(), 5) <= rate:
logger.debug("corrupting now")
result['statusCode'] = _error_code
return result
return func(*args, **kwargs)
return wrapper