|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from abc import ABC, abstractmethod |
| 3 | +from typing import TypedDict |
| 4 | + |
| 5 | +from aws_lambda_powertools.logging.lambda_context import build_lambda_context_model |
| 6 | +from aws_lambda_powertools.utilities.typing import LambdaContext |
| 7 | +from cc_common.config import logger |
| 8 | + |
| 9 | + |
| 10 | +class CustomResourceResponse(TypedDict, total=False): |
| 11 | + """Return body for the custom resource handler.""" |
| 12 | + |
| 13 | + PhysicalResourceId: str |
| 14 | + Data: dict |
| 15 | + NoEcho: bool |
| 16 | + |
| 17 | + |
| 18 | +class CustomResourceHandler(ABC): |
| 19 | + """Base class for custom resource migrations. |
| 20 | +
|
| 21 | + This class provides a framework for implementing CloudFormation custom resources. |
| 22 | + It handles the routing of CloudFormation events to appropriate methods and provides a consistent |
| 23 | + logging pattern. |
| 24 | +
|
| 25 | + Subclasses must implement the on_create, on_update, and on_delete methods. |
| 26 | +
|
| 27 | + Instances of this class are callable and can be used directly as Lambda handlers. |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__(self, handler_name: str): |
| 31 | + """Initialize the custom resource handler. |
| 32 | +
|
| 33 | + :type handler_name: str |
| 34 | + """ |
| 35 | + self.handler_name = handler_name |
| 36 | + |
| 37 | + def __call__(self, event: dict, _context: LambdaContext) -> CustomResourceResponse | None: |
| 38 | + return self._on_event(event, _context) |
| 39 | + |
| 40 | + def _on_event(self, event: dict, _context: LambdaContext) -> CustomResourceResponse | None: |
| 41 | + """CloudFormation event handler using the CDK provider framework. |
| 42 | + See: https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.custom_resources/README.html |
| 43 | +
|
| 44 | + This method routes the event to the appropriate handler method based on the request type. |
| 45 | +
|
| 46 | + :param event: The lambda event with properties in ResourceProperties |
| 47 | + :type event: dict |
| 48 | + :param _context: Lambda context |
| 49 | + :type _context: LambdaContext |
| 50 | + :return: Optional result from the handler method |
| 51 | + :rtype: Optional[CustomResourceResponse] |
| 52 | + :raises ValueError: If the request type is not supported |
| 53 | + """ |
| 54 | + |
| 55 | + # @logger.inject_lambda_context doesn't work on instance methods, so we'll build the context manually |
| 56 | + lambda_context = build_lambda_context_model(_context) |
| 57 | + logger.structure_logs(**lambda_context.__dict__) |
| 58 | + |
| 59 | + logger.info(f'{self.handler_name} handler started') |
| 60 | + |
| 61 | + properties = event.get('ResourceProperties', {}) |
| 62 | + request_type = event['RequestType'] |
| 63 | + |
| 64 | + match request_type: |
| 65 | + case 'Create': |
| 66 | + try: |
| 67 | + resp = self.on_create(properties) |
| 68 | + except Exception as e: |
| 69 | + logger.error(f'Error in {self.handler_name} creation', exc_info=e) |
| 70 | + raise |
| 71 | + case 'Update': |
| 72 | + try: |
| 73 | + resp = self.on_update(properties) |
| 74 | + except Exception as e: |
| 75 | + logger.error(f'Error in {self.handler_name} update', exc_info=e) |
| 76 | + raise |
| 77 | + case 'Delete': |
| 78 | + try: |
| 79 | + resp = self.on_delete(properties) |
| 80 | + except Exception as e: |
| 81 | + logger.error(f'Error in {self.handler_name} delete', exc_info=e) |
| 82 | + raise |
| 83 | + case _: |
| 84 | + raise ValueError(f'Unexpected request type: {request_type}') |
| 85 | + |
| 86 | + logger.info(f'{self.handler_name} handler complete') |
| 87 | + return resp |
| 88 | + |
| 89 | + @abstractmethod |
| 90 | + def on_create(self, properties: dict) -> CustomResourceResponse | None: |
| 91 | + """Handle Create events. |
| 92 | +
|
| 93 | + This method should be implemented by subclasses to perform the migration when a resource is being created. |
| 94 | +
|
| 95 | + :param properties: The ResourceProperties from the CloudFormation event |
| 96 | + :type properties: dict |
| 97 | + :return: Any result to be returned to CloudFormation |
| 98 | + :rtype: Optional[CustomResourceResponse] |
| 99 | + """ |
| 100 | + |
| 101 | + @abstractmethod |
| 102 | + def on_update(self, properties: dict) -> CustomResourceResponse | None: |
| 103 | + """Handle Update events. |
| 104 | +
|
| 105 | + This method should be implemented by subclasses to perform the migration when a resource is being updated. |
| 106 | +
|
| 107 | + :param properties: The ResourceProperties from the CloudFormation event |
| 108 | + :type properties: dict |
| 109 | + :return: Any result to be returned to CloudFormation |
| 110 | + :rtype: Optional[CustomResourceResponse] |
| 111 | + """ |
| 112 | + |
| 113 | + @abstractmethod |
| 114 | + def on_delete(self, properties: dict) -> CustomResourceResponse | None: |
| 115 | + """Handle Delete events. |
| 116 | +
|
| 117 | + This method should be implemented by subclasses to handle deletion of the migration. In many cases, this can |
| 118 | + be a no-op as the migration is temporary and deletion should have no effect. |
| 119 | +
|
| 120 | + :param properties: The ResourceProperties from the CloudFormation event |
| 121 | + :type properties: dict |
| 122 | + :return: Any result to be returned to CloudFormation |
| 123 | + :rtype: Optional[CustomResourceResponse] |
| 124 | + """ |
0 commit comments