如何设置AWS Lambda通过HTTP/API网关异步调用? [英] How can I set an AWS Lambda to be invoked asynchronously through HTTP/API Gateway?

查看:152
本文介绍了如何设置AWS Lambda通过HTTP/API网关异步调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个通过HTTP(即API网关集成)调用的AWS Lambda.

I am writing an AWS Lambda that is invoked via HTTP (i.e. the API Gateway integration).

我希望将该API用作GitHub Webhook.调用webhook/lambda时,我希望立即发送HTTP响应,并希望lambda继续执行(因为这可能需要一些时间).

I wish to use that API as a GitHub webhook. When the webhook/lambda is called I want the HTTP response to be sent right away and the lambda to keep executing (because it can take some time).

这类似于 Event调用类型调用lambda时:

This is similar to the Event invocation type when invoking a lambda:

  • 事件:一发不可收拾
  • RequestResponse:等待响应

是否可以在HTTP集成中使用该调用类型?

Is that possible to use that invocation type with the HTTP integration?

注意:我正在使用 serverless.com 框架.

Note: I am using the serverless.com framework.

推荐答案

@ michael-sqlbot 指出您可以使用

As @michael-sqlbot pointed out you can get this behavior by using the X-Amz-Invocation-Type:Event Header. Getting this setup is a little screwy and the linked documentation is accurate but a little outdated (in my opinion).

  1. 路径替代必须为:/2015-03-31/functions/<ARN TO YOUR LAMBDA>/invocations
  2. 执行角色需要能够调用您的lambda.
  3. 最初设置集成请求时,您无法添加标题,因此必须保存它,然后返回并添加标题.在此处,您可以通过将字符串'Event'放在Mapped from字段中(如下图所示)来将X-Amz-Invocation-Type硬编码为Event
  1. The Path Override needs to be: /2015-03-31/functions/<ARN TO YOUR LAMBDA>/invocations
  2. The Execution Role needs to be able to invoke your lambda.
  3. When initially setting up the Integration Request you cannot add headers so you have to save it, then come back and add headers. From there you can hard code the X-Amz-Invocation-Type to Event by putting the string 'Event' in the Mapped from field (as pictured below)

或者...您也可以通过将连接到API网关的lambda(我们称为lambda A)使用Event调用类型调用另一个lambda(lambda B)来实现此目的.这样,A并不关心B的响应,而是可以在几百毫秒内将成功的响应返回给API网关(假设您没有做太多其他事情).然后Lambda B可以继续运行多长时间(只要它在5分钟lambda限制或您配置的超时之内).

Or... you could also achieve this by having your lambda that's wired up to the API Gateway (we'll call it lambda A) invoke another lambda (lambda B) using the Event invocation type. This way A doesn't care about the response of B and can return a successful response to the API Gateway within several hundred milliseconds (assuming you aren't doing too much else). Then Lambda B can continue running for however long is necessary (as long as it's under the 5 minute lambda limitation or your configured timeout).

这篇关于如何设置AWS Lambda通过HTTP/API网关异步调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆