Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 731
panchicore Avatar asked Dec 15 '15 16:12

panchicore


People also ask

Can API gateway trigger Lambda?

that can call to an HTTP or Websocket endpoint sends that request. these requests are intercepted by API gateway which will take care of managing the traffic of the request. It will send the request to the right service like AWS Lambda, Dynamo DB, EC2 etc. It can be used in mobile or web back ends.

How would you create a request object for your Lambda event from API gateway?

In your AWS Console open up your API Gateway and find the method you want to provide headers. Locate the Integration Request box and click on it to open up these settings. Find the Mapping Templates area of the Integration request and open it up. Add a new mapping template for the application/json Content-Type.


2 Answers

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.

like image 127
Erik Erikson Avatar answered Nov 06 '22 23:11

Erik Erikson


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.

like image 28
panchicore Avatar answered Nov 07 '22 00:11

panchicore