简体   繁体   English

即使在向 API Gateway 发送响应后,AWS Lambda 仍可在后台运行

[英]AWS Lambda to run in background even after sending response to API Gateway

I have searched all through the net but didn't find a solution of how to make this functionality get succeeded.我已经通过网络搜索了所有内容,但没有找到如何使此功能成功的解决方案。 Require help.需要帮助。

My requirement is: I want a that if I trigger an aws lambda function written in node.js and uses an aws-serverless-express module must send back response quickly to API gateway but still should not exit and still run in the backend and we could see cloud watch logs.我的要求是:我想要一个,如果我触发一个用 node.js 编写的 aws lambda 函数并使用 aws-serverless-express 模块必须快速向 API 网关发回响应,但仍然不应该退出并仍然在后端运行,我们可以看到云监视日志。 It must be asynchronous.它必须是异步的。

The code snippet is:代码片段是:

    app.get('/check', function(req, res){
     method.invoke(req)
     res.status(200).send('success')
   })

I did and checked like this but the lambda function gets stopped and returns the response to api gateway it didn't even runs the method.invoke() function in backend.我做了并像这样检查但是 lambda 函数被停止并将响应返回给 api 网关它甚至没有在后端运行 method.invoke() 函数。

Please correct me if anything I am understanding or doing wrong.如果我理解或做错了什么,请纠正我。 I checked with this link: Invoke AWS Lambda and return response to API Gateway asyncronously我检查了这个链接: Invoke AWS Lambda and return response to API Gateway asyncronously

Is it the only way to do this problem.这是解决这个问题的唯一方法。 Creating two lambda functions.创建两个 lambda 函数。

您可以通过使用连接到 API Gateway 的AWS Lambda Step 函数来实现这一点,并行执行具有两个 lambda 函数的分支,其中一个向 API Gateway 返回响应,另一个异步执行。

Besides Step Functions, you could just invoke another Lambda function using the SDK built-in to the Lambda environment.除了 Step Functions 之外,您还可以使用 Lambda 环境中内置的 SDK 调用另一个 Lambda 函数。

I'm no expert in express or NodeJS but I would also think there should be a way to send the HTTP response back and still continue code execution.我不是 express 或 NodeJS 方面的专家,但我也认为应该有一种方法可以将 HTTP 响应发回并继续执行代码。

Can't find a link to the documentation of AWS, but normally it is not possible to continue processing after the Lambda function has returned the response.找不到 AWS 文档的链接,但通常在 Lambda 函数返回响应后无法继续处理。 That's just not how the available runtimes (for the different programming languages) are constructed.这不是可用的运行时(针对不同的编程语言)的构建方式。

Next to invoking separate asynchronous processes (eg, other Lambda function requests, or putting work on a queue) or using AWS Step functions as mentioned here, there's a third method that I know that works: supply a special custom runtime for the AWS Lambda functions that addresses this need.除了调用单独的异步进程(例如,其他 Lambda 函数请求,或将工作放在队列中)或使用这里提到的AWS Step 函数之外,还有第三种我知道有效的方法:为 AWS Lambda 函数提供一个特殊的自定义运行时解决了这一需求。

Next to the standard runtimes, you can create and specify a custom runtime to be used for your AWS Lambda functions.在标准运行时旁边,您可以创建并指定用于 AWS Lambda 函数的自定义运行时 In the standard runtimes, the response of your handler is being posted to the Lambda execution context, after which no activities are possible in your handler because the handler is being terminated (or at least: paused).在标准运行时,您的处理程序的响应被发布到 Lambda 执行上下文,之后您的处理程序中不可能有任何活动,因为处理程序正在终止(或至少:暂停)。

So, the trick to make additional processing possible after sending the response is to move the responsibility of posting the response to the Lambda operating content from the bootstrap script to the Lambda function handler itself... and continue to do your processing in your Lambda function handler after already having sent the response.因此,在发送响应后进行额外处理的技巧是将发布响应到 Lambda 操作内容的责任从引导脚本转移到 Lambda 函数处理程序本身......并继续在你的 Lambda 函数中进行处理已发送响应后的处理程序。 Using your custom runtime, processing in the Lambda functions will then not be terminated after having sent the response, since it's not how your custom runtime is constructed.使用您的自定义运行时,Lambda 函数中的处理将不会在发送响应后终止,因为这不是您的自定义运行时的构造方式。

It's not the architecturally-best solution, as it messes with the responsibilities between the Lambda operating context and your Lambda functions handlers... but it makes it possible to do processing in your Lambda function handlers after having sent the response.它不是架构上最好的解决方案,因为它混淆了 Lambda 操作上下文和您的 Lambda 函数处理程序之间的职责……但它可以在发送响应后在您的 Lambda 函数处理程序中进行处理。

Step function seems the best solution here.阶跃函数在这里似乎是最好的解决方案。 See @Ashan's reply.请参阅@Ashan 的回复。 Apart from that you can use the new invoke method in lambda nodejs sdk.除此之外,您可以在 lambda nodejs sdk 中使用新的 invoke 方法。 Note that invokeAsync is now deprecated.请注意,现在不推荐使用 invokeAsync。 You can set InvocationType to Event See the example below.您可以将 InvocationType 设置为 Event 请参见下面的示例。 which is taken from here这是取自here

var params = {
  ClientContext: "MyApp", 
  FunctionName: "MyFunction", 
  InvocationType: "Event", 
  LogType: "Tail", 
  Payload: <Binary String>, 
  Qualifier: "1"
 };
 lambda.invoke(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
   /*
   data = {
    FunctionError: "", 
    LogResult: "", 
    Payload: <Binary String>, 
    StatusCode: 123
   }
   */
 }); 

one example use case is, first function would return an immediate response, and it would trigger another lambda function which would execute the tasks and eventually may be call a webhook.一个示例用例是,第一个函数将立即返回响应,它将触发另一个 lambda 函数,该函数将执行任务并最终可能调用 webhook。

I am just new here and I want to help so here is how I overcame this problem:我是新来的,我想提供帮助,所以我是如何克服这个问题的:
I did install axios in my javascript code and then did a request on the other function I just added an http api endpoint via serverless and viola stuff was fixed plus I can pass parameters in the request body.我确实在我的 javascript 代码中安装了 axios,然后对另一个函数做了一个请求,我刚刚通过无服务器添加了一个 http api 端点,中提琴的东西被修复了,而且我可以在请求正文中传递参数。

The other's call it fire and forget request.其他人称其为火而忘记的请求。 You should look into it ;)你应该调查一下;)

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

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