如何使用AWS ApiGateway HttpApi调用lambda异步 [英] How to invoke lambda async with AWS ApiGateway HttpApi

查看:283
本文介绍了如何使用AWS ApiGateway HttpApi调用lambda异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AWS Api Gateway的新HTTP Api(于2019年12月推出).

I'm using the new HTTP Api (The one introduced in December 2019) of AWS Api Gateway.

我曾经使用过REST Api,通过添加X-Amz-Invocation-Type:Event标头,您可以异步触发lambda函数.

I used to work with REST Api, which lets you trigger lambda functions async by adding X-Amz-Invocation-Type:Event header.

新的HTTP Api不允许您指定标头,如何异步调用lambda函数?

The new HTTP Api does not let you specify that header, how can I call lambda functions asynchronously?

预先感谢

推荐答案

仅当集成为 Lambda和HTTP端点的代理集成,因此无法在API网关集成配置中设置X-Amz-Invocation-Type标头.

You can invoke a Lambda function asynchronously via API Gateway only if the integration is non-proxy. By default, HTTP APIs are designed to support only proxy integrations for Lambda and HTTP endpoints so it is not possible to set the X-Amz-Invocation-Type header in the API Gateway integration config.

为了通过HTTP API异步调用Lambda,您可以使用两个Lambda函数,其中一个充当实际函数的代理.

In order to invoke a Lambda asynchronously via HTTP API, you can use two Lambda functions with one acting as proxy to your actual function.

HTTP API->调用Lambda1 同步- ->调用Lambda2 异步

HTTP API --> Invoke Lambda1 synchronously --> Invoke Lambda2 asynchronously

下面是Lambda1异步调用Lambda2的示例NodeJS代码片段-

Below is a sample NodeJS code snippet for Lambda1 to invoke Lambda2 asynchronously -

const params = {
        FunctionName: 'FUNCTION_NAME',
        InvocationType: 'Event',
        Payload: JSON.parse(event.body) // this is the event coming from API Gateway to Lambda1
    };
await lambda.invoke(params).promise(); // await here is only going to wait for the HTTP request to be successful. Once Lambda2 is invoked, it will return immediately

这篇关于如何使用AWS ApiGateway HttpApi调用lambda异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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