简体   繁体   中英

Asynchronous call in AWS Lambda

I am actually a bit confused about how asynchronous process happens in AWS Lambda functions.

Normally in a sync call, a request is made through the API Gateway which directly linked to the the Lambda function and waits for the Lambda function to return a response. But in async, a request that passes through the API Gateway may end up in an SQS queue, and a temporary response of successfully placed into the queue (which may or may not necessary depends on implementation) is returned to the user without waiting for the actual Lambda function response to be returned to the user. Then the Lambda function will asynchronously consume request in the queue and return a response.

The problem is -- where does this response go?

I know it will go to downstream resource like Lambda with a function url or EventBridge or another SQS queue, but what if there is no downstream resource? The whole thing is just a API Gateway + Lambda + SQS.

Does the response get immediately returned from the API Gateway? How will the user catch it in that way since they are not waiting for a response? Or if they do, how do they wait for it? Do they call something like await if they use typescript CDK?

It depends what you mean by response. API Gateway will receive a response (when called async) from the lambda that the request has been received. But the result is processed downstream depending of what you have configured. With Lambda destinations you can direct the result further but API gateway is not able to fetch your result.

If you want to receive that result as a calling client you'll have to use iE a websocket. What makes often sense is to use some storage first pattern and return the messageId from sqs to the caller and register a route on api gateway to receive the result with that messageId. For a more managed solution you might use AppSync

Here is a good resource for solutions: https://aws.amazon.com/blogs/compute/building-storage-first-applications-with-http-apis-service-integrations/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM