|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +# SPDX-License-Identifier: MIT-0 |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +import boto3 |
| 8 | +import requests |
| 9 | +import signal |
| 10 | +import sys |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +# global variables |
| 15 | +# extension name has to match the file's parent directory name) |
| 16 | +LAMBDA_EXTENSION_NAME = Path(__file__).parent.name |
| 17 | + |
| 18 | + |
| 19 | +# custom extension code |
| 20 | +def execute_custom_processing(event): |
| 21 | + # perform custom per-event processing here |
| 22 | + print(f"[{LAMBDA_EXTENSION_NAME}] Received event: {json.dumps(event)}", flush=True) |
| 23 | + |
| 24 | + |
| 25 | +# boiler plate code |
| 26 | +def handle_signal(signal, frame): |
| 27 | + # if needed pass this signal down to child processes |
| 28 | + print(f"[{LAMBDA_EXTENSION_NAME}] Received signal={signal}. Exiting.", flush=True) |
| 29 | + sys.exit(0) |
| 30 | + |
| 31 | + |
| 32 | +def register_extension(): |
| 33 | + print(f"[{LAMBDA_EXTENSION_NAME}] Registering...", flush=True) |
| 34 | + headers = { |
| 35 | + 'Lambda-Extension-Name': LAMBDA_EXTENSION_NAME, |
| 36 | + } |
| 37 | + payload = { |
| 38 | + 'events': [ |
| 39 | + 'INVOKE', |
| 40 | + 'SHUTDOWN' |
| 41 | + ], |
| 42 | + } |
| 43 | + response = requests.post( |
| 44 | + url=f"http://{os.environ['AWS_LAMBDA_RUNTIME_API']}/2020-01-01/extension/register", |
| 45 | + json=payload, |
| 46 | + headers=headers |
| 47 | + ) |
| 48 | + ext_id = response.headers['Lambda-Extension-Identifier'] |
| 49 | + print(f"[{LAMBDA_EXTENSION_NAME}] Registered with ID: {ext_id}", flush=True) |
| 50 | + |
| 51 | + return ext_id |
| 52 | + |
| 53 | + |
| 54 | +def process_events(ext_id, client): |
| 55 | + headers = { |
| 56 | + 'Lambda-Extension-Identifier': ext_id |
| 57 | + } |
| 58 | + |
| 59 | + while True: |
| 60 | + print(f"[{LAMBDA_EXTENSION_NAME}] Waiting for event...", flush=True) |
| 61 | + |
| 62 | + response = requests.get( |
| 63 | + url=f"http://{os.environ['AWS_LAMBDA_RUNTIME_API']}/2020-01-01/extension/event/next", |
| 64 | + headers=headers, |
| 65 | + timeout=None |
| 66 | + ) |
| 67 | + event = json.loads(response.text) |
| 68 | + if event['eventType'] == 'SHUTDOWN': |
| 69 | + print(f"[{LAMBDA_EXTENSION_NAME}] Received SHUTDOWN event. Exiting.", flush=True) |
| 70 | + sys.exit(0) |
| 71 | + else: |
| 72 | + execute_custom_processing(event) |
| 73 | + |
| 74 | +client = None |
| 75 | + |
| 76 | +def main(): |
| 77 | + global client |
| 78 | + if client is None: |
| 79 | + client = boto3.client('ssm') |
| 80 | + |
| 81 | + # handle signals |
| 82 | + signal.signal(signal.SIGINT, handle_signal) |
| 83 | + signal.signal(signal.SIGTERM, handle_signal) |
| 84 | + |
| 85 | + # execute extensions logic |
| 86 | + extension_id = register_extension() |
| 87 | + process_events(extension_id, client) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments