Giter Site home page Giter Site logo

adhorn / aws-lambda-chaos-injection Goto Github PK

View Code? Open in Web Editor NEW
99.0 9.0 13.0 274 KB

Chaos Injection library for AWS Lambda

License: MIT License

Python 95.33% Makefile 2.37% Batchfile 2.30%
serveless chaos-engineering chaos-monkey aws lambda-functions amazon-web-services sre testing

aws-lambda-chaos-injection's People

Contributors

adhorn avatar alexcasalboni avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aws-lambda-chaos-injection's Issues

Fix order of events

Executing the lambda handler in all cases and only over-writing the error code if a valid parameter result is received means that the function code will be invoked and will falsely report an error instead of not being invoked and reporting a fake error.

if _fault_type == "status_code":
result = func(*args, **kwargs)
if isinstance(_chaos_conf.get("error_code"), int):
_error_code = _chaos_conf.get("error_code")

The simple solution would be to fix the order of operations. The structurally better solution would be to rebuild the whole library with a hooks pattern where the customer could define a list of faults/frequencies to inject before and after function execution.

Support for local configuration file

Using SSM is great, but maybe someone would prefer not having additional latency just to read the configuration.

Supporting a local configuration file or even just reading the configuration from an environment variable could be a useful alternative.

Dynamic exception and message for @inject_exception

I'd expect to be able to configure something like this:

@inject_exception(TypeError)
def lambda_handler_with_type_error(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello from Lambda!'
    }

@inject_exception(ValueError)
def lambda_handler_with_value_error(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello from Lambda!'
    }

@inject_exception(ValueError, "Custom message here")
def lambda_handler_with_custom_message(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello from Lambda!'
    }

Handle AccessDenied

When the Lambda function doesn't have enough permissions to read the SSM parameter, you get an AccessDeniedException ("botocore.exceptions.ClientError: An error occurred (AccessDeniedException)"). We could handle that here.

This way we could show customers a proper error such as "Your Lambda function is missing the required IAM permissions to read the chaos configuration from SSM" - and maybe even provide them with the correct JSON document, as documented here.

Dynamic delay for @inject_delay

I'd expect to be able to configure something like this:

@inject_delay(400)
def lambda_handler_with_short_delay(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello from Lambda!'
    }

@inject_delay(2000)
def lambda_handler_with_long_delay(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello from Lambda!'
    }

Improve tests

A few considerations about tests:

  1. I think we shouldn't test for printed output in our tests, as it doesn't allow you to easily change and/or add new logs.

  2. also, we could use a library such as moto to avoid actually API calls during unit tests, so anybody can run unit tests without configuring AWS credentials. Additionally, we can always run the same tests as "integration tests" on a real AWS account.

  3. we could speed up all the tests related to delays by patching time.sleep. This is not super critical now, but it'd allow us to add more tests and test much longer intervals without waiting minutes to unit tests to complete.

For example, this:

@ignore_warnings
def test_get_delay(self):
    with patch('sys.stdout', new=StringIO()) as fakeOutput:
        response = handler('foo', 'bar')
        assert (
            'Injecting 400 of delay with a rate of 1' in fakeOutput.getvalue().strip()
        )
    self.assertEqual(str(response), "{'statusCode': 200, 'body': 'Hello from Lambda!'}")

could simply become this:

def test_get_delay(self):
    with patch('time.sleep', return_value=None) as patched_time_sleep:
        response = handler('foo', 'bar')
        patched_time_sleep. assert_called()
    self.assertEqual(str(response), "{'statusCode': 200, 'body': 'Hello from Lambda!'}")

And we could mock SSM like this:

from moto import mock_ssm

@mock_ssm
class TestExceptionMethods(unittest.TestCase):
    ...

Dynamic status code for @inject_statuscode

I'd expect to be able to configure something like this:

@inject_statuscode(301)
def lambda_handler_with_301(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello from Lambda!'
    }

@inject_statuscode(400)
def lambda_handler_with_300(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello from Lambda!'
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.