import json import boto3 from datetime import datetime def lambda_handler(event, context): record = event['Records'][0] event_name = record['eventName'] # ObjectCreated:Put o ObjectRemoved:Delete file_name = record['s3']['object']['key'] bucketName = record['s3']['bucket']['name'] event_time = record["eventTime"] # Cliente SES ses = boto3.client("ses") # Styles para colores del icono según evento if event_name.startswith("ObjectCreated"): action = "File Uploaded" color = "#16a34a" # verde emoji = "🟢" elif event_name.startswith("ObjectRemoved"): action = "File Deleted" color = "#dc2626" # rojo emoji = "🔴" else: action = "S3 Event Triggered" color = "#2563eb" # azul emoji = "🔵" # Asunto dinámico subject = f"[S3 Notification] {action} - {bucketName}" # Cuerpo HTML (bonito y elegante) body = f"""
An S3 event has been detected in your bucket. Below are the details:
| Bucket | {bucketName} |
| File | {file_name} |
| Event | {event_name} |
| Datetime | {event_time} |
This message was generated automatically by your AWS Lambda function.