Skip to content

Commit 6186fe7

Browse files
authored
Created alternate function
Existing function does not leverage the SQS messages in the lambda event and instead polls the queue again, meaning once function gets up to speed with concurrent invocations, the queue is perpetually empty: only 5-7 messages make it into DynamoDB, then no more. This fix instead uses the Lambda event to push messages into DynamoDB.
1 parent 33f15a4 commit 6186fe7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from datetime import datetime
2+
import json
3+
import os
4+
import boto3
5+
6+
DYNAMODB_TABLE = os.environ['DYNAMODB_TABLE']
7+
8+
dynamodb = boto3.resource('dynamodb')
9+
10+
def lambda_handler(event, context):
11+
# Count items in the Lambda event
12+
no_messages = str(len(event['Records']))
13+
print("Found " +no_messages +" messages to process.")
14+
15+
for message in event['Records']:
16+
17+
print(message)
18+
19+
# Write message to DynamoDB
20+
table = dynamodb.Table(DYNAMODB_TABLE)
21+
22+
response = table.put_item(
23+
Item={
24+
'MessageId': message['messageId'],
25+
'Body': message['body'],
26+
'Timestamp': datetime.now().isoformat()
27+
}
28+
)
29+
print("Wrote message to DynamoDB:", json.dumps(response))

0 commit comments

Comments
 (0)