Skip to content

Commit 1cbb721

Browse files
committed
Updated changes from Mike's comments
1 parent 04ba313 commit 1cbb721

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

serverless-message-processing/README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
An adaptable pattern for message processing using AWS serverless services, featuring error handling and automatic recovery mechanisms.
55

66
## Core Components
7-
- API Gateway (message ingestion)
8-
- SQS Queues (main + DLQs)
7+
- Amazon API Gateway (message ingestion)
8+
- Amazon SQS Queues (main + DLQs)
99
- Lambda Functions (processing + recovery)
1010

1111

@@ -24,6 +24,17 @@ An adaptable pattern for message processing using AWS serverless services, featu
2424

2525
## Deployment
2626
# Build the SAM application
27+
28+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
29+
```
30+
git clone https://github.com/aws-samples/serverless-patterns
31+
```
32+
2. Change directory to the pattern directory:
33+
```
34+
cd serverless-patterns/serverless-messaging-redrive
35+
```
36+
37+
# Build the SAM application
2738
The ```sam build ``` command prepares an application for subsequent steps in the developer workflow, such as local testing or deploying to AWS.
2839
2940
```

serverless-message-processing/functions/decision_maker/app.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def validate_fixed_email(email):
6666
Returns:
6767
bool: True if valid email format, False otherwise
6868
"""
69-
email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
69+
email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\$'
7070
return bool(re.match(email_pattern, email))
7171

7272
def lambda_handler(event, context):
@@ -93,15 +93,17 @@ def lambda_handler(event, context):
9393
event: Lambda event object containing SQS messages
9494
context: Lambda context object
9595
Returns:
96-
dict: Processing summary with counts
96+
dict: Processing summary with counts and batchItemFailures
9797
"""
9898
processed_count = 0
9999
fixed_count = 0
100100
fatal_count = 0
101+
failed_message_ids = []
101102

102103
logger.info(f"Starting to process batch of {len(event['Records'])} messages")
103104

104105
for record in event['Records']:
106+
original_message_id = "unknown"
105107
try:
106108
# Parse the failed message
107109
message = json.loads(record['body'])
@@ -158,6 +160,8 @@ def lambda_handler(event, context):
158160

159161
except Exception as e:
160162
logger.error(f"Error processing message {original_message_id}: {str(e)}")
163+
# Add message ID to failed messages list
164+
failed_message_ids.append(record['messageId'])
161165
try:
162166
error_message = {
163167
'originalMessage': record['body'],
@@ -171,22 +175,25 @@ def lambda_handler(event, context):
171175
fatal_count += 1
172176
except Exception as fatal_e:
173177
logger.critical(f"Could not send to fatal DLQ: {str(fatal_e)}")
174-
raise
175178

176179
# Execution summary
177180
logger.info(f"""
178181
=== Execution Summary ===
179182
Messages Processed: {processed_count}
180183
Messages Fixed: {fixed_count}
181184
Messages Fatal: {fatal_count}
185+
Messages Failed: {len(failed_message_ids)}
182186
========================
183187
""")
184188

185-
return {
186-
'statusCode': 200,
187-
'body': json.dumps({
189+
# Return both the processing info and the batch failures for SQS
190+
result = {
191+
'batchItemFailures': [{"itemIdentifier": message_id} for message_id in failed_message_ids],
192+
'processingInfo': {
188193
'processed': processed_count,
189194
'fixed': fixed_count,
190195
'fatal': fatal_count
191-
})
196+
}
192197
}
198+
199+
return result

serverless-message-processing/functions/processor/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def validate_email(email):
1414
Returns:
1515
bool: True if valid email format, False otherwise
1616
"""
17-
email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\$'
17+
email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
1818
if not bool(re.match(email_pattern, email)):
1919
logger.error(f"Invalid email format: {email}")
2020
return False

serverless-message-processing/template.yaml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
AWSTemplateFormatVersion: '2010-09-09'
22
Transform: AWS::Serverless-2016-10-31
3-
Description: Automated Serverless Messaging Redrive
3+
Description: Automated Serverless Messaging Redrive (uksb-1tthgi812) (tag:serverless-message-processing)
44

55
Parameters:
66
ProcessorMemorySize:
@@ -136,7 +136,7 @@ Resources:
136136
Type: AWS::Serverless::Function
137137
Properties:
138138
Handler: app.lambda_handler
139-
Runtime: python3.11
139+
Runtime: python3.13
140140
CodeUri: ./functions/processor/
141141
MemorySize: !Ref ProcessorMemorySize
142142
Timeout: !Ref LambdaTimeout
@@ -147,6 +147,9 @@ Resources:
147147
Properties:
148148
Queue: !GetAtt MainQueue.Arn
149149
BatchSize: 10
150+
FunctionResponseTypes:
151+
- ReportBatchItemFailures
152+
150153
Environment:
151154
Variables:
152155
MAIN_QUEUE_URL: !Ref MainQueue
@@ -156,7 +159,7 @@ Resources:
156159
Type: AWS::Serverless::Function
157160
Properties:
158161
Handler: app.lambda_handler
159-
Runtime: python3.11
162+
Runtime: python3.13
160163
CodeUri: ./functions/decision_maker/
161164
MemorySize: !Ref DecisionMakerMemorySize
162165
Timeout: !Ref LambdaTimeout
@@ -167,9 +170,11 @@ Resources:
167170
Properties:
168171
Queue: !GetAtt AutomatedDLQ.Arn
169172
BatchSize: 10
173+
FunctionResponseTypes:
174+
- ReportBatchItemFailures
175+
176+
170177
Policies:
171-
- SQSPollerPolicy:
172-
QueueName: !GetAtt MainQueue.QueueName
173178
- SQSPollerPolicy:
174179
QueueName: !GetAtt FatalDLQ.QueueName
175180
- SQSSendMessagePolicy:

0 commit comments

Comments
 (0)