From cd5f70b59fe3745f4787fd32afb6bdb4a2deef85 Mon Sep 17 00:00:00 2001 From: Hugo Alvarado Date: Tue, 3 Sep 2019 18:19:46 -0600 Subject: [PATCH 1/4] CloudWatch events are not handled by Zappa --- tests/test_handler.py | 4 ++++ zappa/handler.py | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/test_handler.py b/tests/test_handler.py index c16c65f06..fc2ccba3e 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -416,3 +416,7 @@ def test_merge_headers_no_single_value(self): merged = merge_headers(event) self.assertEqual(merged['a'], 'c, d') self.assertEqual(merged['x'], 'y, z, f') + + + def test_foo(self): + pass diff --git a/zappa/handler.py b/zappa/handler.py index ceebe42db..f9193c6a3 100644 --- a/zappa/handler.py +++ b/zappa/handler.py @@ -343,7 +343,7 @@ def get_function_for_cognito_trigger(self, trigger): def handler(self, event, context): """ An AWS Lambda function which parses specific API Gateway input into a - WSGI request, feeds it to our WSGI app, procceses the response, and returns + WSGI request, feeds it to our WSGI app, processes the response, and returns that back to the API Gateway. """ @@ -463,6 +463,20 @@ def handler(self, event, context): logger.error("Cannot find a function to handle cognito trigger {}".format(triggerSource)) return result + # This is a CloudWatch event + # Related: https://github.com/Miserlou/Zappa/issues/205 + elif event.get('awslogs', None): + result = None + whole_function = '{}.{}'.format(settings.APP_MODULE, settings.APP_FUNCTION) + app_function = self.import_module_and_get_function(whole_function) + if app_function: + result = self.run_function(app_function, event, context) + logger.debug("Result of %s:" % whole_function) + logger.debug(result) + else: + logger.error("Cannot find a function to process the triggered event.") + return result + # Normal web app flow try: # Timing From b05c1843ac2a4342eef53762630b4203d2daa805 Mon Sep 17 00:00:00 2001 From: Hugo Alvarado Date: Tue, 3 Sep 2019 18:40:43 -0600 Subject: [PATCH 2/4] Update issue link --- zappa/handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zappa/handler.py b/zappa/handler.py index f9193c6a3..311e62f0f 100644 --- a/zappa/handler.py +++ b/zappa/handler.py @@ -464,7 +464,7 @@ def handler(self, event, context): return result # This is a CloudWatch event - # Related: https://github.com/Miserlou/Zappa/issues/205 + # Related: https://github.com/Miserlou/Zappa/issues/1924 elif event.get('awslogs', None): result = None whole_function = '{}.{}'.format(settings.APP_MODULE, settings.APP_FUNCTION) From 20f69e2c9b68c65b364c4720b02347267ae22509 Mon Sep 17 00:00:00 2001 From: Hugo Alvarado Date: Tue, 3 Sep 2019 19:29:17 -0600 Subject: [PATCH 3/4] Add test --- tests/test_handler.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/test_handler.py b/tests/test_handler.py index fc2ccba3e..ce2d47acd 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -417,6 +417,22 @@ def test_merge_headers_no_single_value(self): self.assertEqual(merged['a'], 'c, d') self.assertEqual(merged['x'], 'y, z, f') + def test_cloudwatch_subscription_event(self): + """ + Test that events sent in the format used by CloudWatch logs via + subscription filters are handled properly. + The actual payload that Lambda receives is in the following format + { "awslogs": {"data": "BASE64ENCODED_GZIP_COMPRESSED_DATA"} } + https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/SubscriptionFilters.html + """ + lh = LambdaHandler('tests.test_wsgi_script_name_settings') + + event = { + 'awslogs': { + 'data': "some-data-not-important-for-test" + } + } + response = lh.handler(event, None) - def test_foo(self): - pass + self.assertEqual(response['statusCode'], 200) + self.assertEqual(response['statusDescription'], '200 OK') From 0e8d9d1541faac1b57621d5bb85565084288e908 Mon Sep 17 00:00:00 2001 From: Hugo Alvarado Date: Tue, 3 Sep 2019 21:09:20 -0600 Subject: [PATCH 4/4] Add specific test config and handler for the cloudwatch events --- tests/test_event_script_app.py | 6 ++++++ tests/test_event_script_settings.py | 10 ++++++++++ tests/test_handler.py | 5 ++--- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 tests/test_event_script_app.py create mode 100644 tests/test_event_script_settings.py diff --git a/tests/test_event_script_app.py b/tests/test_event_script_app.py new file mode 100644 index 000000000..5ec978e67 --- /dev/null +++ b/tests/test_event_script_app.py @@ -0,0 +1,6 @@ +from __future__ import print_function + + +def handler_for_events(event, context): + print('Event:', event) + return True diff --git a/tests/test_event_script_settings.py b/tests/test_event_script_settings.py new file mode 100644 index 000000000..8e748c4b2 --- /dev/null +++ b/tests/test_event_script_settings.py @@ -0,0 +1,10 @@ +API_STAGE = 'dev' +APP_FUNCTION = 'handler_for_events' +APP_MODULE = 'tests.test_event_script_app' +DEBUG = 'True' +DJANGO_SETTINGS = None +DOMAIN = 'api.example.com' +ENVIRONMENT_VARIABLES = {} +LOG_LEVEL = 'DEBUG' +PROJECT_NAME = 'test_event_script_app' +COGNITO_TRIGGER_MAPPING = {} diff --git a/tests/test_handler.py b/tests/test_handler.py index ce2d47acd..64caff083 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -425,7 +425,7 @@ def test_cloudwatch_subscription_event(self): { "awslogs": {"data": "BASE64ENCODED_GZIP_COMPRESSED_DATA"} } https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/SubscriptionFilters.html """ - lh = LambdaHandler('tests.test_wsgi_script_name_settings') + lh = LambdaHandler('tests.test_event_script_settings') event = { 'awslogs': { @@ -434,5 +434,4 @@ def test_cloudwatch_subscription_event(self): } response = lh.handler(event, None) - self.assertEqual(response['statusCode'], 200) - self.assertEqual(response['statusDescription'], '200 OK') + self.assertEqual(response, True)