Skip to main content

Decorators and Class to inject failures into AWS Lambda functions

Project description

chaos_lambda is a small library injecting chaos into AWS Lambda. It offers simple python decorators to do delay, exception and statusCode injection and a Class to add delay to any 3rd party dependencies called from your function. This allows to conduct small chaos engineering experiments for your serverless application in the AWS Cloud.

  • Support for Latency injection using delay
  • Support for Exception injection using exception_msg
  • Support for HTTP Error status code injection using error_code
  • Using for SSM Parameter Store to control the experiment using isEnabled
  • Support for adding rate of failure using rate. (Default rate = 1)
  • Per Lambda function injection control using Environment variable (CHAOS_PARAM)


pip install chaos-lambda



from chaos_lambda import (
inject_delay, inject_exception, inject_statuscode)

os.environ['CHAOS_PARAM'] = 'chaoslambda.config'

def lambda_handler_with_exception(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello from Lambda!'

def lambda_handler_with_delay(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello from Lambda!'

def lambda_handler_with_statuscode(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello from Lambda!'

When excecuted, the Lambda function, e.g lambda_handler_with_exception('foo', 'bar'), will produce the following result:

exception_msg from config I really failed seriously with a rate of 1
corrupting now
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../", line 199, in wrapper
    raise Exception(exception_msg)
Exception: I really failed seriously


The configuration for the failure injection is stored in the AWS SSM Parameter Store and accessed at runtime by the get_config() function:

    "isEnabled": true,
    "delay": 400,
    "error_code": 404,
    "exception_msg": "I really failed seriously",
    "rate": 1

To store the above configuration into SSM using the AWS CLI do the following:

aws ssm put-parameter --region eu-north-1 --name chaoslambda.config --type String --overwrite --value "{ "delay": 400, "isEnabled": true, "error_code": 404, "exception_msg": "I really failed seriously", "rate": 1 }"

AWS Lambda will need to have IAM access to SSM.

    "Version": "2012-10-17",
    "Statement": [
            "Effect": "Allow",
            "Action": [
            "Resource": "*"
            "Effect": "Allow",
            "Action": [
            "Resource": "arn:aws:ssm:eu-north-1:12345678910:parameter/chaoslambda.config"

Supported Decorators:

chaos_lambda currently supports the following decorators:

  • @inject_delay - add delay in the AWS Lambda execution
  • @inject_exception - Raise an exception during the AWS Lambda execution
  • @inject_statuscode - force AWS Lambda to return a specific HTTP error code

and the following class:

  • SessionWithDelay - enabled to sub-classing requests library and call dependencies with delay

More information:

Full Documentation

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for chaos-lambda, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size chaos_lambda-0.1.2-py3-none-any.whl (6.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size chaos-lambda-0.1.2.tar.gz (4.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page