-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
fix message retry in lambda SQS event source mapping #6603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
tests/integration/awslambda/functions/lambda_sqs_integration.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""This lambda is used for lambda/sqs integration tests. Since SQS event source mappings don't allow | ||
DestinationConfigurations that send lambda results to other source (like SQS queues), that can be used to verify | ||
invocations, this lambda does this manually. You can pass in an event that looks like this:: | ||
|
||
{ | ||
"destination": "<q FFAD ueue_url>", | ||
"fail_attempts": 2 | ||
} | ||
|
||
Which will cause the lambda to fail twice (comparing the "ApproximateReceiveCount" of the SQS event triggering | ||
the lambda), and send either an error or success result to the SQS queue passed in the destination key. | ||
""" | ||
import json | ||
import os | ||
|
||
import boto3 | ||
|
||
|
||
def handler(event, context): | ||
# this lambda expects inputs from an SQS event source mapping | ||
if len(event.get("Records", [])) != 1: | ||
raise ValueError("the payload must consist of exactly one record") | ||
|
||
# it expects exactly one record where the message body is '{"destination": "<queue_url>"}' that mimics a | ||
# DestinationConfig (which is not possible with SQS event source mappings). | ||
record = event["Records"][0] | ||
message = json.loads(record["body"]) | ||
|
||
if not message.get("destination"): | ||
raise ValueError("no destination for the event given") | ||
|
||
error = None | ||
try: | ||
if message["fail_attempts"] >= int(record["attributes"]["ApproximateReceiveCount"]): | ||
raise ValueError("failed attempt") | ||
except Exception as e: | ||
error = e | ||
raise | ||
finally: | ||
# we then send a message to the destination queue | ||
result = {"error": None if not error else str(error), "event": event} | ||
sqs = create_external_boto_client("sqs") | ||
sqs.send_message(QueueUrl=message.get("destination"), MessageBody=json.dumps(result)) | ||
|
||
|
||
def create_external_boto_client(service): | ||
endpoint_url = None | ||
if os.environ.get("LOCALSTACK_HOSTNAME"): | ||
endpoint_url = ( | ||
f"http://{os.environ['LOCALSTACK_HOSTNAME']}:{os.environ.get('EDGE_PORT', 4566)}" | ||
) | ||
return boto3.client(service, endpoint_url=endpoint_url) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not 100% sure but I think this needs to have a value set for
WaitTimeSeconds
since per default the SQS poller uses long polling (for non-FIFO queues at least) according to https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#events-sqs-scalingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In localstack that might not be particularly relevant though because AFAIK we don't differentiate between short and long polling anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
totally agree. unfortunately we can't block here because of the way the loop is structured. see comment here: #5042 (comment)
that would require a thread per event-source-mapping, which again requires a different interface for the event source listener (adding/removing mappings). i'd love to do that as part of the lambda rework.