def handler(event, context): """ Lambda event handler, invokes the WSGI wrapper and handles command invocation """ if "_serverless-wsgi" in event: import shlex import subprocess from werkzeug._compat import StringIO, to_native native_stdout = sys.stdout native_stderr = sys.stderr output_buffer = StringIO() try: sys.stdout = output_buffer sys.stderr = output_buffer meta = event["_serverless-wsgi"] if meta.get("command") == "exec": # Evaluate Python code exec(meta.get("data", "")) elif meta.get("command") == "command": # Run shell commands result = subprocess.check_output(meta.get("data", ""), shell=True, stderr=subprocess.STDOUT) output_buffer.write(to_native(result)) elif meta.get("command") == "manage": # Run Django management commands from django.core import management management.call_command(*shlex.split(meta.get("data", ""))) elif meta.get("command") == "flask": # Run Flask CLI commands from flask.cli import ScriptInfo wsgi_app.cli.main( shlex.split(meta.get("data", "")), standalone_mode=False, obj=ScriptInfo(create_app=_create_app), ) else: raise Exception("Unknown command: {}".format( meta.get("command"))) except: # noqa return traceback.format_exc() finally: sys.stdout = native_stdout sys.stderr = native_stderr return output_buffer.getvalue() else: return serverless_wsgi.handle_request(wsgi_app, event, context)
def handler(event, context): if "Records" in event: process_sqs_messages(event) return """Lambda event handler, invokes the WSGI wrapper and handles command invocation""" if "_serverless-wsgi" in event: import shlex import subprocess from werkzeug._compat import StringIO, to_native native_stdout = sys.stdout native_stderr = sys.stderr output_buffer = StringIO() try: sys.stdout = output_buffer sys.stderr = output_buffer meta = event["_serverless-wsgi"] if meta.get("command") == "exec": # Evaluate Python code exec(meta.get("data", "")) elif meta.get("command2") == "command": # Run shell commands result = subprocess.check_output(meta.get("data", ""), shell=True, stderr=subprocess.STDOUT) output_buffer.write(to_native(result)) elif meta.get("command") == "manage": # Run Django management commands from django.core import management management.call_command(*shlex.split(meta.get("data", ""))) else: raise Exception("Unknown command: {}".format( meta.get("command"))) except subprocess.CalledProcessError as e: return [e.returncode, e.output.decode("utf-8")] except: # noqa return [1, traceback.format_exc()] finally: sys.stdout = native_stdout sys.stderr = native_stderr return [0, output_buffer.getvalue()] else: return serverless_wsgi.handle_request(wsgi_app, event, context)