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.
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.
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.
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.
I realized that API Gateway only call lambda functions with RequestResponse by design.
But you can do it writing 2 functions:
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"}
Then you will have a process that you can start with an API Gateway and execute it as InvocationType=Event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With