|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from random import randint |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import boto3 |
| 7 | + |
| 8 | +from aws_lambda_powertools import Logger |
| 9 | +from aws_lambda_powertools.utilities.batch import ( |
| 10 | + BasePartialBatchProcessor, |
| 11 | + EventType, |
| 12 | + process_partial_response, |
| 13 | +) |
| 14 | + |
| 15 | +table_name = os.getenv("TABLE_NAME", "table_not_found") |
| 16 | + |
| 17 | +logger = Logger() |
| 18 | + |
| 19 | + |
| 20 | +class MyPartialProcessor(BasePartialBatchProcessor): |
| 21 | + """ |
| 22 | + Process a record and stores successful results at a Amazon DynamoDB Table |
| 23 | +
|
| 24 | + Parameters |
| 25 | + ---------- |
| 26 | + table_name: str |
| 27 | + DynamoDB table name to write results to |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__(self, table_name: str): |
| 31 | + self.table_name = table_name |
| 32 | + |
| 33 | + super().__init__(event_type=EventType.SQS) |
| 34 | + |
| 35 | + def _prepare(self): |
| 36 | + # It's called once, *before* processing |
| 37 | + # Creates table resource and clean previous results |
| 38 | + self.ddb_table = boto3.resource("dynamodb").Table(self.table_name) |
| 39 | + self.success_messages.clear() |
| 40 | + |
| 41 | + def _clean(self): |
| 42 | + # It's called once, *after* closing processing all records (closing the context manager) |
| 43 | + # Here we're sending, at once, all successful messages to a ddb table |
| 44 | + with self.ddb_table.batch_writer() as batch: |
| 45 | + for result in self.success_messages: |
| 46 | + batch.put_item(Item=result) |
| 47 | + |
| 48 | + def _process_record(self, record): |
| 49 | + # It handles how your record is processed |
| 50 | + # Here we're keeping the status of each run |
| 51 | + # where self.handler is the record_handler function passed as an argument |
| 52 | + try: |
| 53 | + result = self.handler(record) # record_handler passed to decorator/context manager |
| 54 | + return self.success_handler(record, result) |
| 55 | + except Exception as exc: |
| 56 | + logger.error(exc) |
| 57 | + return self.failure_handler(record, sys.exc_info()) |
| 58 | + |
| 59 | + def success_handler(self, record, result: Any): |
| 60 | + entry = ("success", result, record) |
| 61 | + self.success_messages.append(record) |
| 62 | + return entry |
| 63 | + |
| 64 | + async def _async_process_record(self, record: dict): |
| 65 | + raise NotImplementedError() |
| 66 | + |
| 67 | + |
| 68 | +processor = MyPartialProcessor(table_name) |
| 69 | + |
| 70 | + |
| 71 | +def record_handler(record): |
| 72 | + return randint(0, 100) |
| 73 | + |
| 74 | + |
| 75 | +def lambda_handler(event, context): |
| 76 | + return process_partial_response(event=event, record_handler=record_handler, processor=processor, context=context) |
0 commit comments