- Newest
- Most votes
- Most comments
You can refactor your application to using asynchronous pattern:
- frontend post request to a queue/bus (e.g. SQS) and some sort of a request token is returned to the frontend immediately (e.g. a message id)
- long running Lambda function is triggered from the queue/bus and runs for more than 30 seconds
- once it completes, is saves the result to a data store, e.g. a DynamoDB Table, optionally with a TTL for that record, so that it can be automatically deleted. Make sure to use the request token as the search key (e.g. PK) for the result, so that it can be easily retrieved by the frontend
- frontend polls backend API for the result given the previously generated token (see step 1). Alternatively you can use notify the frontend once the response is ready by using such services as AWS AppSync Events, API Gateway WebSocket APIs, or Amazon SNS.
Although this approach is more complicated than a simple synchronous Lambda call, it will allow you to get results from processes running longer than the maximum API response timeout.
answered a year ago
Lambda can run for as long as 15 minutes. The call from the frontend to the API Gateway is the real problem. For synchronous requests, API Gateway has a timeout of about 30 seconds. If your Lambda takes longer than that, API Gateway will terminate the connection and the frontend will time out. However, the Lambda will continue running and complete the work in the background. This is why the frontend will time out while the Lambda will say "Success."
Based on your situation, there are two recommended approaches for handling long-running Lambda executions in Amplify:
-
Run the Lambda asynchronously in the background. Instead of waiting for a long-running Lambda, let the frontend call an endpoint that immediately returns a jobId, and let the frontend fetch the result later. AWS docs: https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway-tasks.html
-
Use AWS AppSync. AWS Amplify supports asynchronous AWS Lambda calls from the AWS AppSync API. This feature allows you to call Lambda functions asynchronously, allowing your API to perform more complex and long-running operations without blocking the response to the client. https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/#async-function-handlers
answered 10 months ago
Relevant content
asked 5 years ago
- AWS OFFICIALUpdated 4 months ago
