bootstrap 884 Bytes
Newer Older
1 2 3 4 5 6 7
#!/usr/bin/python36

import importlib
import json
import sys
import os

8
sys.path.insert(0, "/var/task")
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
import requests

AWS_LAMBDA_RUNTIME_API = os.environ.get("AWS_LAMBDA_RUNTIME_API")
LAMBDA_TASK_ROOT = os.environ.get("LAMBDA_TASK_ROOT")
HANDLER = os.environ.get("_HANDLER")
sys.path.append(LAMBDA_TASK_ROOT)

handler_parts = HANDLER.split(".")
handler_module = importlib.__import__(handler_parts[0])
handler_function = handler_parts[1]

while True:
    event_data = requests.get(
        f"http://{AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next"
    )
    request_id = event_data.headers["Lambda-Runtime-Aws-Request-Id"]

    response_url = f"http://{AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/{request_id}/response"
    handler_args = {"event": event_data.json(), "response_url": response_url}
    getattr(handler_module, handler_function)(**handler_args)