|
7 | 7 | from aws_lambda_powertools.utilities.data_classes import SQSEvent, event_source |
8 | 8 | from aws_lambda_powertools.utilities.typing.lambda_context import LambdaContext |
9 | 9 | from boto3 import client |
| 10 | +from botocore.config import Config |
| 11 | +from botocore.exceptions import ClientError |
10 | 12 |
|
11 | 13 | from .change_event_validation import validate_change_event |
12 | 14 | from common.dynamodb import add_change_event_to_dynamodb, get_latest_sequence_id_for_a_given_odscode_from_dynamodb |
13 | 15 | from common.middlewares import redact_staff_key_from_event, unhandled_exception_logging |
14 | 16 | from common.types import HoldingQueueChangeEventItem |
15 | 17 | from common.utilities import extract_body, get_sequence_number |
16 | 18 |
|
| 19 | +# Configure boto3 client with explicit timeout to prevent hanging in Lambda |
| 20 | +boto_config = Config(connect_timeout=10, read_timeout=15) |
| 21 | + |
17 | 22 | logger = Logger() |
18 | 23 | tracer = Tracer() |
19 | | -sqs = client("sqs") |
| 24 | +# Create SQS client at module level for connection reuse across Lambda invocations |
| 25 | +sqs_client = client("sqs", config=boto_config) |
20 | 26 |
|
21 | 27 |
|
22 | 28 | @redact_staff_key_from_event() |
@@ -82,8 +88,17 @@ def lambda_handler(event: SQSEvent, context: LambdaContext) -> None: # noqa: AR |
82 | 88 | correlation_id=logger.get_correlation_id(), |
83 | 89 | ) |
84 | 90 | logger.debug("Change event validated", holding_queue_change_event_item=holding_queue_change_event_item) |
85 | | - sqs.send_message( |
86 | | - QueueUrl=getenv("HOLDING_QUEUE_URL"), |
87 | | - MessageBody=dumps(holding_queue_change_event_item), |
88 | | - MessageGroupId=ods_code, |
89 | | - ) |
| 91 | + try: |
| 92 | + sqs_client.send_message( |
| 93 | + QueueUrl=getenv("HOLDING_QUEUE_URL"), |
| 94 | + MessageBody=dumps(holding_queue_change_event_item), |
| 95 | + MessageGroupId=ods_code, |
| 96 | + ) |
| 97 | + except ClientError as err: |
| 98 | + logger.error( |
| 99 | + "Failed to send message to holding queue", |
| 100 | + error=str(err), |
| 101 | + error_code=err.response["Error"]["Code"], |
| 102 | + queue_url=getenv("HOLDING_QUEUE_URL"), |
| 103 | + ) |
| 104 | + raise |
0 commit comments