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

aws lambda invoke example
invoke lambda function from another lambda function nodejs
aws api gateway asynchronous lambda
call lambda from lambda
aws lambda triggers
call aws lambda function from python
manually invoke lambda function
call api gateway from lambda

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.

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.

Set up asynchronous invocation of the backend Lambda function , Configure Lambda asynchronous invocation in the API Gateway console Clients can include the InvocationType: Event header in API requests for  When you invoke a function, you can choose to invoke it synchronously or asynchronously. With synchronous invocation, you wait for the function to process the event and return a response. With asynchronous invocation, Lambda queues the event for processing and returns a response immediately. For asynchronous invocation, Lambda handles

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.

How to invoke Lambda function with Event Invocation Type via API , 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. A Lambda integration maps a path and HTTP method combination to a Lambda function. You can configure API Gateway to pass the body of the HTTP request as-is (custom integration), or to encapsulate the request body in a document that includes all of the request information including headers, resource, path, and method.

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

Tutorial: Using AWS Lambda with Amazon API Gateway, Create two Lambdas and in the first use Lambda.Client.invoke with InvocationType=Event in an dedicated ApiGateway request handling Lambda. The second  When you invoke a function synchronously, Lambda runs the function and waits for a response. When the function execution ends, Lambda returns the response from the function's code with additional data, such as the version of the function that was executed. To invoke a function synchronously with the AWS CLI, use the invoke command.

Invocation Methods and Integrations, In this example you create a simple API using Amazon API Gateway. Invoke the function manually using the sample event data. synchronous execution—​invokes the Lambda function using the RequestResponse invocation type). We recommend that you invoke the function using the console because the console UI provides a user-friendly interface for reviewing the execution results, including the execution summary, logs written by your code, and the results returned by the function (because the console always performs synchronous execution—invokes the Lambda function

Learning Lambda, Different ways of invoking a Lambda function and integrating to other a function synchronously, the API consumer provides the InvocationType parameter as RequestResponse . Asynchronous. When you invoke a function asynchronously, Lambda sends the event to a Using AWS Lambda with Amazon API Gateway ↩​︎. Invokes a Lambda function. You can invoke a function synchronously (and wait for the response), or asynchronously. To invoke a function asynchronously, set InvocationType to Event .

Executing and Invoking Lambda Function, We call a Lambda function asynchronously by using the Event invocation-type. aws lambda invoke --invocation-type Event --function-name MyLambda busses (SNS, Kinesis), HTTP servers (API Gateway), and more. When you invoke the lambda locally or through the Lambda console, you are invoking that lambda directly and so your lambda receives exactly what you're sending.. When you invoke it through API Gateway, API Gateway creates the event object for you based on your HTTP request.

Comments
  • This answer is the correct one and should be accepted
  • @jeffrey-- Could you also please suggest similar question posted here-stackoverflow.com/questions/53504395/… Unable to fetch messages sent from AWS MQ and send to another Lambda function
  • That solution is works, but why using InvocationType:Event header not start Lambda in async mode?
  • so invoke_async doesn't actually allow you to do anything with the response? that seems useless...
  • @AndyHayden the definition of asynchronous is to initiate a process outside of the normal chronological flow and continue processing without waiting for a response. So, no, you cannot do anything with the response. In javascript they get around this by passing a callback to the function. You could do this with AWS Lambda, but it would have to be a callback that the lambda function has access to.
  • @BrunoBronosky IME in practice you rarely want to "fire and forget". You can get around this be requesting the url from the API gateway (with an async http request). Although I forget what I was doing in November to want this / whether that is what I ended up doing.
  • "rarely want" is very subjective. Every time you have a long running process and you don't want to wait on it, you want to "fire and forget". In that case you are effectively using Lambda as a message queue. For example, transcoding a video. You want to quickly tell the user "transcoding in progress" which the 202 status code of the Event invocation type tells you is true. You don't want to keep the request to lambda open for 30 minutes or so. You'd be better off polling for results or using a callback.