|
| 1 | +import boto3 |
| 2 | +import json |
| 3 | +import logging |
| 4 | +import os |
| 5 | +from decimal import Decimal |
| 6 | + |
| 7 | +# --- Configuration --- |
| 8 | +logging.basicConfig( |
| 9 | + level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" |
| 10 | +) |
| 11 | + |
| 12 | +AWS_REGION = os.environ.get("AWS_REGION", "us-east-1") |
| 13 | +DYNAMODB_TABLE_NAME = "infra-core-api-audit-log" |
| 14 | +FIREHOSE_STREAM_NAME = "infra-core-api-audit-log-stream" |
| 15 | + |
| 16 | +# The top-level attributes to include in the JSON record for Firehose |
| 17 | +REQUIRED_KEYS = ["module", "createdAt", "actor", "message", "requestId", "target"] |
| 18 | + |
| 19 | +# Kinesis Data Firehose has a batch limit of 500 records per call. |
| 20 | +FIREHOSE_BATCH_SIZE = 500 |
| 21 | + |
| 22 | +# --- CORRECTED: Primary key configuration based on your schema. --- |
| 23 | +DYNAMODB_PRIMARY_KEY_ATTRIBUTES = ["module", "createdAt"] |
| 24 | + |
| 25 | + |
| 26 | +class DecimalEncoder(json.JSONEncoder): |
| 27 | + """A helper class to convert DynamoDB's Decimal type to standard int/float for JSON.""" |
| 28 | + |
| 29 | + def default(self, obj): |
| 30 | + if isinstance(obj, Decimal): |
| 31 | + return int(obj) if obj % 1 == 0 else float(obj) |
| 32 | + return super(DecimalEncoder, self).default(obj) |
| 33 | + |
| 34 | + |
| 35 | +def _process_and_delete_batch( |
| 36 | + firehose_batch, dynamodb_items_batch, firehose_client, dynamodb_table |
| 37 | +): |
| 38 | + """ |
| 39 | + Sends a batch of records to Firehose and deletes the successful ones from DynamoDB. |
| 40 | + Returns the count of records successfully sent and deleted. |
| 41 | + """ |
| 42 | + if not firehose_batch: |
| 43 | + return 0, 0 |
| 44 | + |
| 45 | + sent_count = 0 |
| 46 | + deleted_count = 0 |
| 47 | + |
| 48 | + try: |
| 49 | + # 1. Send the entire batch to Firehose |
| 50 | + response = firehose_client.put_record_batch( |
| 51 | + DeliveryStreamName=FIREHOSE_STREAM_NAME, Records=firehose_batch |
| 52 | + ) |
| 53 | + |
| 54 | + failed_put_count = response.get("FailedPutCount", 0) |
| 55 | + if failed_put_count > 0: |
| 56 | + logging.warning( |
| 57 | + f"{failed_put_count} of {len(firehose_batch)} records failed to be " |
| 58 | + "sent to Firehose in this batch. They will not be deleted." |
| 59 | + ) |
| 60 | + |
| 61 | + # 2. Identify successful records and prepare their keys for deletion |
| 62 | + keys_to_delete = [] |
| 63 | + for i, record_response in enumerate(response.get("RequestResponses", [])): |
| 64 | + original_item = dynamodb_items_batch[i] |
| 65 | + request_id = original_item.get("requestId", "N/A") |
| 66 | + |
| 67 | + if "ErrorCode" in record_response: |
| 68 | + # This record failed, log the error and skip deletion |
| 69 | + logging.error( |
| 70 | + f"Failed to send record {request_id} to Firehose: " |
| 71 | + f"{record_response.get('ErrorCode')} - {record_response.get('ErrorMessage')}" |
| 72 | + ) |
| 73 | + else: |
| 74 | + # This record succeeded, add its primary key to the deletion list |
| 75 | + try: |
| 76 | + primary_key = { |
| 77 | + key: original_item[key] |
| 78 | + for key in DYNAMODB_PRIMARY_KEY_ATTRIBUTES |
| 79 | + } |
| 80 | + keys_to_delete.append(primary_key) |
| 81 | + except KeyError as ke: |
| 82 | + logging.error( |
| 83 | + f"Sent record {request_id} but cannot delete. " |
| 84 | + f"Missing primary key attribute in item: {ke}. " |
| 85 | + "Please check DYNAMODB_PRIMARY_KEY_ATTRIBUTES configuration." |
| 86 | + ) |
| 87 | + |
| 88 | + sent_count = len(keys_to_delete) |
| 89 | + |
| 90 | + # 3. Use a batch_writer to efficiently delete all successful records from DynamoDB |
| 91 | + if keys_to_delete: |
| 92 | + with dynamodb_table.batch_writer() as batch: |
| 93 | + for key in keys_to_delete: |
| 94 | + batch.delete_item(Key=key) |
| 95 | + deleted_count = len(keys_to_delete) |
| 96 | + logging.info( |
| 97 | + f"Successfully sent and deleted {deleted_count} records in this batch." |
| 98 | + ) |
| 99 | + |
| 100 | + except Exception as e: |
| 101 | + logging.error(f"A fatal error occurred while processing a batch: {e}") |
| 102 | + # In case of a total batch failure, nothing is sent or deleted. |
| 103 | + return 0, 0 |
| 104 | + |
| 105 | + return sent_count, deleted_count |
| 106 | + |
| 107 | + |
| 108 | +def process_send_and_delete_logs(): |
| 109 | + """ |
| 110 | + Scans a DynamoDB table, sends items in batches to Kinesis Data Firehose, |
| 111 | + and deletes successfully processed items from the DynamoDB table. |
| 112 | + """ |
| 113 | + if not DYNAMODB_PRIMARY_KEY_ATTRIBUTES: |
| 114 | + logging.error( |
| 115 | + "Configuration error: DYNAMODB_PRIMARY_KEY_ATTRIBUTES is not set." |
| 116 | + ) |
| 117 | + return |
| 118 | + |
| 119 | + try: |
| 120 | + dynamodb = boto3.resource("dynamodb", region_name=AWS_REGION) |
| 121 | + table = dynamodb.Table(DYNAMODB_TABLE_NAME) |
| 122 | + firehose = boto3.client("firehose", region_name=AWS_REGION) |
| 123 | + |
| 124 | + total_sent_count = 0 |
| 125 | + total_deleted_count = 0 |
| 126 | + logging.info( |
| 127 | + f"Starting scan of '{DYNAMODB_TABLE_NAME}' to process in batches of {FIREHOSE_BATCH_SIZE}..." |
| 128 | + ) |
| 129 | + |
| 130 | + firehose_batch = [] |
| 131 | + dynamodb_items_batch = [] |
| 132 | + scan_kwargs = {} |
| 133 | + done = False |
| 134 | + start_key = None |
| 135 | + |
| 136 | + while not done: |
| 137 | + if start_key: |
| 138 | + scan_kwargs["ExclusiveStartKey"] = start_key |
| 139 | + |
| 140 | + response = table.scan(**scan_kwargs) |
| 141 | + items = response.get("Items", []) |
| 142 | + |
| 143 | + for item in items: |
| 144 | + # Build the record for the Firehose batch |
| 145 | + output_record = {key: item.get(key) for key in REQUIRED_KEYS} |
| 146 | + payload = (json.dumps(output_record, cls=DecimalEncoder) + "\n").encode( |
| 147 | + "utf-8" |
| 148 | + ) |
| 149 | + firehose_batch.append({"Data": payload}) |
| 150 | + |
| 151 | + # Keep the original item to get its primary key for deletion later |
| 152 | + dynamodb_items_batch.append(item) |
| 153 | + |
| 154 | + # If the batch is full, process it |
| 155 | + if len(firehose_batch) >= FIREHOSE_BATCH_SIZE: |
| 156 | + sent, deleted = _process_and_delete_batch( |
| 157 | + firehose_batch, dynamodb_items_batch, firehose, table |
| 158 | + ) |
| 159 | + total_sent_count += sent |
| 160 | + total_deleted_count += deleted |
| 161 | + # Clear the batches for the next set of records |
| 162 | + firehose_batch = [] |
| 163 | + dynamodb_items_batch = [] |
| 164 | + |
| 165 | + start_key = response.get("LastEvaluatedKey", None) |
| 166 | + done = start_key is None |
| 167 | + |
| 168 | + # Process any remaining records that are left over after the loop finishes |
| 169 | + if firehose_batch: |
| 170 | + logging.info( |
| 171 | + f"Processing the final batch of {len(firehose_batch)} records..." |
| 172 | + ) |
| 173 | + sent, deleted = _process_and_delete_batch( |
| 174 | + firehose_batch, dynamodb_items_batch, firehose, table |
| 175 | + ) |
| 176 | + total_sent_count += sent |
| 177 | + total_deleted_count += deleted |
| 178 | + |
| 179 | + logging.info( |
| 180 | + f"Scan complete. Total records sent: {total_sent_count}. " |
| 181 | + f"Total records deleted: {total_deleted_count}." |
| 182 | + ) |
| 183 | + |
| 184 | + except ( |
| 185 | + boto3.client("dynamodb").exceptions.ResourceNotFoundException, |
| 186 | + dynamodb.meta.client.exceptions.ResourceNotFoundException, |
| 187 | + ): |
| 188 | + logging.error(f"Error: DynamoDB table '{DYNAMODB_TABLE_NAME}' not found.") |
| 189 | + except firehose.exceptions.ResourceNotFoundException: |
| 190 | + logging.error(f"Error: Firehose stream '{FIREHOSE_STREAM_NAME}' not found.") |
| 191 | + except Exception as e: |
| 192 | + logging.error(f"An unexpected error occurred: {e}") |
| 193 | + |
| 194 | + |
| 195 | +if __name__ == "__main__": |
| 196 | + process_send_and_delete_logs() |
0 commit comments