How to call a Lambda function with an event call type through an API gateway?

The docs say:

By default, the Invoke API uses the RequestResponse call type. You can request asynchronous execution by specifying the event as 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 not Async ... docs also says:

When you call the Lambda function through the AWS console or through HTTPS using the Amazon API Gateway, Lambda always uses the RequestResponse call type.

I know that this is possible using aws-CLI, that I do not understand if it is possible to do this from the endpoint of the API gateway.

+4
source share
3 answers

, API Gateway - RequestResponse .

, 2 :

  • 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. " Executer", .

, API InvocationType = Event.

+1

Lambdas Lambda.Client.invoke InvocationType = ApiGateway, Lambda. , , ApiGateway .

Lambda ApiGateway:

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" }

, , , .

p.s. , invoke_async
p.p.s. , , , : 0. , ; 1. api; 2. ( , ) InvocationType = 'Event' .

+10

Source: https://habr.com/ru/post/1620162/


All Articles