- Newest
- Most votes
- Most comments
The easiest way to serialize lambda executions (prevent parallel execution) is to put a queue between your API Gateway and Lambda. If you use SQS, you'll need to implement FIFO queues (https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/FIFO-queues-exactly-once-processing.html) or SNS FIFO topics. Additionally, you will need to set the reserve concurrency of the function to 1. However, you will also need to handle 429 errors when you function is throttled by using a dead-letter queue and reprocessing logic.
However, you should also think about your overall architecture. One of the main benefits of Lambdas are their ability to scale to handle parallel execution. In order for this to work, you need to observe some basic design tenants including idempotency. If serialized execution of lambda function is really what you are after, I would argue that lambdas is probably not the right choice for compute. Take a look at this blog for more details. https://aws.amazon.com/blogs/compute/handling-lambda-functions-idempotency-with-aws-lambda-powertools/
When you configure API Gateway to invoke a Lambda function, every time you invoke the API, it will invoke your function. If you have some reasons that do not allow you to run the function in parallel, you have two main options:
- Return an error to the client when it invokes the function while it is already running. Let the client retry. You can do that by setting a concurrency limit of 1 on the function. (API Gateway -> Lambda)
- Place the message into a queue and let Lambda process the messages from the queue. In this case the client will not get an error, but you still need to limit the concurrency to 1. (API Gateway -> SQS -> Lambda)
If you do not want the client to retry and you don'n need the answer from the function, you can go with the second option. Otherwise, you can go with the first one.
Relevant content
- asked 2 months ago
- asked 2 years ago
- Accepted Answerasked 2 years ago
- What's the difference between Lambda function execution role permissions and invocation permissions?AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated a year ago