简体   繁体   English

AWS Lambda 中的异步调用

[英]Asynchronous call in AWS Lambda

I am actually a bit confused about how asynchronous process happens in AWS Lambda functions.实际上,我对 AWS Lambda 函数中的异步过程如何发生有点困惑。

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. 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.但是在异步中,通过 API 网关的请求可能最终进入 SQS 队列,并且成功放入队列的临时响应(可能需要也可能不需要取决于实现)返回给用户,而无需等待实际 Lambda function 响应返回给用户。 Then the Lambda function will asynchronously consume request in the queue and return a response.然后 Lambda function 将异步消费队列中的请求并返回响应。

The problem is -- where does this response go?问题是——这个响应 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? 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.整个事情只是一个 API 网关 + Lambda + SQS。

Does the response get immediately returned from the API Gateway?响应是否会立即从 API 网关返回? 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?如果他们使用 typescript CDK,他们会调用类似 await 的东西吗?

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. API 网关将收到来自 lambda 的请求已收到的响应(当称为异步时)。 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.使用 Lambda 目的地,您可以进一步引导结果,但 API 网关无法获取您的结果。

If you want to receive that result as a calling client you'll have to use iE a websocket.如果您想作为调用客户端接收该结果,则必须使用 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.通常有意义的是使用一些存储优先模式并将 messageId 从 sqs 返回给调用者,并在 api 网关上注册路由以接收带有该 messageId 的结果。 For a more managed solution you might use AppSync对于更受管理的解决方案,您可以使用 AppSync

Here is a good resource for solutions: https://aws.amazon.com/blogs/compute/building-storage-first-applications-with-http-apis-service-integrations/这是一个很好的解决方案资源: https://aws.amazon.com/blogs/compute/building-storage-first-applications-with-http-apis-service-integrations/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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