How to invoke lambda async with AWS ApiGateway HttpApi

拥有回忆 提交于 2020-07-03 10:15:12

问题


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

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

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

thanks in advance


回答1:


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.

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 --> Invoke Lambda1 synchronously --> Invoke Lambda2 asynchronously

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


来源:https://stackoverflow.com/questions/61081164/how-to-invoke-lambda-async-with-aws-apigateway-httpapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!