How to invoke Lambda function with Event Invocation Type via API Gateway?

▼魔方 西西 提交于 2019-12-12 08:13:48

问题


Docs says:

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

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:

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

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


回答1:


Create two Lambdas and in the first use Lambda.Client.invoke with InvocationType=Event in an dedicated ApiGateway request handling Lambda. The second executes the logic you want the ApiGateway request to asynchronously invoke.

Example dedicated ApiGateway Lambda handler:

from __future__ import print_function

import boto3
import json

def lambda_handler(event, context):
    response = client.invoke(
        FunctionName='<your_long_task_running_function_executer>',
        InvocationType='Event',
        Payload=json.dumps(event)
    )
    return { "result": "OK" }

You would likely want to detect a failure to send the request and other conditions of that sort but as I don't primarily use python, I'll leave that logic up to you.

p.s. note that invoke_async is deprecated
p.p.s. sorry, my account is new and I don't have the rep to add these as a comment: 0. I borrowed from what you answered; 1. you are using a deprecated api; and 2. you ought (clearly it's been fine) to add InvocationType = 'Event' into your call.




回答2:


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

But you can do it writing 2 functions:

  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. A "Function Executer" that executes the long running task.

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




回答3:


According to this article you can pass a header: X-Amz-Invocation-Type: Event https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html



来源:https://stackoverflow.com/questions/34294693/how-to-invoke-lambda-function-with-event-invocation-type-via-api-gateway

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