import os import json import boto3 import logging from pprint import pformat from checks import get_check def lambda_handler(event, context): """Handler expects event of format { "s3_paths": [], "check_types": [] } """ log_level = os.environ.get("LOG_LEVEL", "INFO") env = os.getenv("ENV", "local") topic_arn = os.getenv("TOPIC_ARN", "local") subject = os.getenv("SUBJECT", "S3 Monitor lambda") if logging.getLogger().hasHandlers(): logging.getLogger().setLevel(log_level) else: logging.basicConfig(level=log_level) if env == "local": session = boto3.Session(profile_name=event["profile"]) s3_client = session.client("s3") sns_client = session.client("sns") else: s3_client = boto3.client("s3") sns_client = boto3.client("sns") report = {} for path in event["s3_paths"]: report[path] = {} for check_type in event["check_types"]: check = get_check(check_type) report[path][check_type] = check(s3_client, path) # Generate readable message for email consumers msg = "" for path, checks in report.items(): msg += f"Path prefix: {path}\n" for check, status in checks.items(): status_str = "Passed" if status else "Failed" msg += f"Check: {check} Status: {status_str} \n" msg += "\n" logging.info(msg) # If any of checks has failed, send notification for _, v in report.items(): for _, check_status in v.items(): if not check_status: response = sns_client.publish(TopicArn=topic_arn, Message=msg, Subject = subject) logging.info(response) return """ if __name__ == "__main__": file_path = "s3_monitor_lambda/example_config.json" with open(file_path, "r") as file: config = json.load(file) lambda_handler(config, {}) """