(event, context)
| 18 | |
| 19 | # Lambda handler |
| 20 | def handler(event, context): |
| 21 | try: |
| 22 | host = os.environ["COLLECTION_ENDPOINT"] |
| 23 | if host.startswith("https://"): |
| 24 | host = host[8:] |
| 25 | region = os.environ["REGION"] |
| 26 | service = "aoss" |
| 27 | credentials = boto3.Session().get_credentials() |
| 28 | |
| 29 | awsauth = AWS4Auth( |
| 30 | credentials.access_key, |
| 31 | credentials.secret_key, |
| 32 | region, |
| 33 | service, |
| 34 | session_token=credentials.token, |
| 35 | ) |
| 36 | |
| 37 | # Build the OpenSearch client |
| 38 | os_client = OpenSearch( |
| 39 | hosts=[{"host": host, "port": 443}], |
| 40 | http_auth=awsauth, |
| 41 | use_ssl=True, |
| 42 | verify_certs=False, |
| 43 | connection_class=RequestsHttpConnection, |
| 44 | timeout=300, |
| 45 | ) |
| 46 | |
| 47 | cw_data = str(event["awslogs"]["data"]) |
| 48 | cw_logs = gzip.GzipFile( |
| 49 | fileobj=BytesIO(base64.b64decode(cw_data, validate=True)) |
| 50 | ).read() |
| 51 | cw_logs = json.loads(cw_logs) |
| 52 | if cw_logs["messageType"] == "CONTROL_MESSAGE": |
| 53 | print("Skipping control message") |
| 54 | return |
| 55 | |
| 56 | parse_and_send(os_client, cw_logs) |
| 57 | |
| 58 | except Exception as e: |
| 59 | logging.exception("Failed to process CloudWatch data") |
| 60 | response_status = "FAILED" |
| 61 | error_message = f"{response_status} Error: {str(e)}. " |
| 62 | print(error_message) |
| 63 | finally: |
| 64 | return "Succeeded" |
| 65 | |
| 66 | |
| 67 | def parse_and_send(os_client, cw_logs): |
nothing calls this directly
no test coverage detected