Skip to content

Commit

Permalink
Merge pull request #1925 from hugoalvarado/master
Browse files Browse the repository at this point in the history
CloudWatch events are not handled by Zappa
  • Loading branch information
jneves committed Feb 29, 2020
2 parents 379c0bf + 0e8d9d1 commit ba20c85
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
6 changes: 6 additions & 0 deletions tests/test_event_script_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from __future__ import print_function


def handler_for_events(event, context):
print('Event:', event)
return True
10 changes: 10 additions & 0 deletions tests/test_event_script_settings.py
Original file line number Diff line number Diff line change
@@ -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 = {}
19 changes: 19 additions & 0 deletions tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,22 @@ 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_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_event_script_settings')

event = {
'awslogs': {
'data': "some-data-not-important-for-test"
}
}
response = lh.handler(event, None)

self.assertEqual(response, True)
16 changes: 15 additions & 1 deletion zappa/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,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.
"""
Expand Down Expand Up @@ -458,6 +458,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/1924
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
Expand Down

0 comments on commit ba20c85

Please sign in to comment.