-
-
Save heitorlessa/4aad06c39a1d520ff8c42adc72b0bcd5 to your computer and use it in GitHub Desktop.
Lambda Dummy Extension POC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import signal | |
import sys | |
import time | |
from aws_lambda_powertools import Logger, Metrics, Tracer | |
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver | |
from aws_lambda_powertools.logging import correlation_paths | |
import os | |
import json | |
import urllib3 | |
import threading | |
logger = Logger(service="APP") | |
tracer = Tracer(service="APP") | |
metrics = Metrics(namespace="MyApp", service="APP") | |
app = ApiGatewayResolver() | |
http = urllib3.PoolManager() | |
#### DUMMY EXTENSION SECTION | |
EXTENSION_HOST = os.getenv("AWS_LAMBDA_RUNTIME_API") | |
EXTENSION_ENDPOINT = f"http://{EXTENSION_HOST}/2020-01-01/extension" | |
EXTENSION_NEXT_EVENT_URL = f"{EXTENSION_ENDPOINT}/event/next" | |
EXTENSION_REGISTER_URL = f"{EXTENSION_ENDPOINT}/register" | |
EXTENSION_ID_HEADER = "Lambda-Extension-Identifier" | |
EXTENSION_REGISTRATION_HEADERS = { | |
"Lambda-Extension-Name": "dummy", | |
"Content-Type": "application/json", | |
} | |
CLEANUP_ENDPOINT = ( | |
"https://jfhth8lff7.execute-api.eu-west-1.amazonaws.com/Prod/shutdown" | |
) | |
def register_dummy_extension(): | |
def activate_extension(extension_id: str): | |
## a single /next call activates the extension | |
http.request( | |
method="GET", | |
url=EXTENSION_NEXT_EVENT_URL, | |
headers={"Lambda-Extension-Identifier": extension_id}, | |
) | |
logger.info("Extension activated", extension_id=extension_id}) | |
logger.info("Registering the extension") | |
registration = http.request( | |
method="POST", | |
url=EXTENSION_REGISTER_URL, | |
headers=EXTENSION_REGISTRATION_HEADERS, | |
body=json.dumps({"events": []}), | |
) | |
logger.info("Activating extension", extension_id=extension_id, registration=registration.data) | |
extension_id = registration.headers[EXTENSION_ID_HEADER] | |
threading.Thread(target=activate_extension, args=[extension_id]).start() ## a must have | |
def register_cleanup_handler(): | |
def cleanup(signal, frame): | |
logger.info("Received SIGTERM; shutting down...") | |
http.request(method="GET", url=CLEANUP_ENDPOINT) | |
sys.exit(0) | |
signal.signal(signal.SIGTERM, cleanup) | |
register_dummy_extension() | |
register_cleanup_handler() | |
#### DUMMY EXTENSION SECTION | |
@app.get("/hello") | |
@tracer.capture_method | |
def hello(): | |
time.sleep(5) # force timeout to receive SIGTERM | |
return {"message": "hello stranger!"} | |
@tracer.capture_lambda_handler | |
@logger.inject_lambda_context( | |
correlation_id_path=correlation_paths.API_GATEWAY_REST, log_event=True | |
) | |
@metrics.log_metrics(capture_cold_start_metric=True) | |
def lambda_handler(event, context): | |
try: | |
return app.resolve(event, context) | |
except Exception as e: | |
logger.exception(e) | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample using Batch processing to demonstrate POC in a SQS long-processing example