如何通过API网关使用事件调用类型调用Lambda函数? [英] How to invoke Lambda function with Event Invocation Type via API Gateway?

查看:101
本文介绍了如何通过API网关使用事件调用类型调用Lambda函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说:

默认情况下,Invoke API假定为RequestResponse调用类型.您可以选择将Event指定为InvocationType来请求异步执行.

By default, the Invoke API assumes RequestResponse invocation type. You can optionally request asynchronous execution by specifying Event as the InvocationType.

因此我可以发送到我的函数(python)的所有地方都是InvocationType:Event:

So all I can send to my function (python) is InvocationType:Event everywhere:

curl -X POST "https://X.execute-api.us-east-1.amazonaws.com/prod/Y?InvocationType=Event" 
-d "InvocationType:Event" 
-H "X-Amz-Invocation-Type:Event"

(function sleeps 3 seconds then responses)

null

但不是异步...文档还说:

But is not Async... docs also says:

当您通过AWS控制台或使用Amazon API Gateway通过HTTPS调用Lambda函数时,Lambda始终使用RequestResponse调用类型.

When you invoke a Lambda function via the AWS console or over HTTPS using Amazon API Gateway, Lambda always uses the RequestResponse invocation type.

我知道通过aws-CLI可以实现这一点,我不知道是否可以从API网关端点进行此操作.

I know that can be possible via aws-CLI, what I dont understand if it is possible to do it from API Gateway endpoint.

推荐答案

我意识到API网关在设计上仅使用RequestResponse调用lambda函数.

I realized that API Gateway only call lambda functions with RequestResponse by design.

但是您可以编写2个函数来实现它:

But you can do it writing 2 functions:

  1. 一个函数接收器",它调用对函数执行器"的异步调用

  1. A "Function Receiver" that invokes the async call to a "Function Executer"

from __future__ import print_function

import boto3
import json

def lambda_handler(event, context):
    client = boto3.client('lambda')
    client.invoke_async(
        FunctionName='my_long_task_running_function_executer',
        InvokeArgs=json.dumps(event)
    )
    return {"result": "OK"}

  1. 执行长时间运行任务的函数执行器".

然后,您将具有一个过程,可以从API网关开始并以InvocationType = Event的身份执行它.

Then you will have a process that you can start with an API Gateway and execute it as InvocationType=Event.

这篇关于如何通过API网关使用事件调用类型调用Lambda函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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