An AWS Lambda function which parses specific API Gateway input into a WSGI request, feeds it to our WSGI app, processes the response, and returns that back to the API Gateway.
(self, event, context)
| 685 | return self.settings.COGNITO_TRIGGER_MAPPING.get(trigger) |
| 686 | |
| 687 | def handler(self, event, context): |
| 688 | """ |
| 689 | An AWS Lambda function which parses specific API Gateway input into a |
| 690 | WSGI request, feeds it to our WSGI app, processes the response, and returns |
| 691 | that back to the API Gateway. |
| 692 | |
| 693 | """ |
| 694 | settings = self.settings |
| 695 | # If in DEBUG mode, log all raw incoming events. |
| 696 | if settings.DEBUG: |
| 697 | logger.debug("Zappa Event: {}".format(event)) |
| 698 | |
| 699 | # Set any API Gateway defined Stage Variables |
| 700 | # as env vars |
| 701 | if event.get("stageVariables"): |
| 702 | for key in event["stageVariables"]: |
| 703 | os.environ[key] = event["stageVariables"][key] |
| 704 | |
| 705 | # This is the result of a keep alive, recertify |
| 706 | # or scheduled event. |
| 707 | if event.get("detail-type") == "Scheduled Event": |
| 708 | whole_function = event["resources"][0].split("/")[-1].split("-")[-1] |
| 709 | |
| 710 | # This is a scheduled function. |
| 711 | if "." in whole_function: |
| 712 | app_function = self.import_module_and_get_function(whole_function) |
| 713 | |
| 714 | # Execute the function! |
| 715 | return self.run_function(app_function, event, context) |
| 716 | |
| 717 | # Else, let this execute as it were. |
| 718 | |
| 719 | # This is a direct command invocation. |
| 720 | elif event.get("command", None): |
| 721 | whole_function = event["command"] |
| 722 | app_function = self.import_module_and_get_function(whole_function) |
| 723 | result = self.run_function(app_function, event, context) |
| 724 | print("Result of %s:" % whole_function) |
| 725 | print(result) |
| 726 | return result |
| 727 | |
| 728 | # This is a direct, raw python invocation. |
| 729 | # It's _extremely_ important we don't allow this event source |
| 730 | # to be overridden by unsanitized, non-admin user input. |
| 731 | elif event.get("raw_command", None): |
| 732 | raw_command = event["raw_command"] |
| 733 | exec(raw_command) |
| 734 | return |
| 735 | |
| 736 | # This is a Django management command invocation. |
| 737 | elif event.get("manage", None): |
| 738 | from django.core import management |
| 739 | |
| 740 | try: # Support both for tests |
| 741 | from zappa.ext.django_zappa import get_django_wsgi |
| 742 | except ImportError: # pragma: no cover |
| 743 | from django_zappa_app import get_django_wsgi |
| 744 |