Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set an AWS Lambda to be invoked asynchronously through HTTP/API Gateway?

I am writing an AWS Lambda that is invoked via HTTP (i.e. the API Gateway integration).

I wish to use that API as a GitHub webhook. When the webhook/lambda is called I want the HTTP response to be sent right away and the lambda to keep executing (because it can take some time).

This is similar to the Event invocation type when invoking a lambda:

  • Event: fire and forget
  • RequestResponse: wait for the response

Is that possible to use that invocation type with the HTTP integration?

Note: I am using the serverless.com framework.

like image 209
Matthieu Napoli Avatar asked May 10 '18 11:05

Matthieu Napoli


People also ask

Can API gateway invoke Lambda asynchronously?

You can invoke a Lambda function asynchronously via API Gateway only if the integration is non-proxy. By default, HTTP APIs are designed to support only proxy integrations for Lambda and HTTP endpoints so it is not possible to set the X-Amz-Invocation-Type header in the API Gateway integration config.

Is it possible to make a Lambda that executes asynchronously?

Lambda functions can be invoked either synchronously or asynchronously, depending upon the trigger. In synchronous invocations, the caller waits for the function to complete execution and the function can return a value.

Can you invoke a cross account Lambda function from an API gateway integration?

You can now use an AWS Lambda function from a different AWS account as your API integration backend. Each account can be in any region where Amazon API Gateway is available. This makes it easy to centrally manage and share Lambda backend functions across multiple APIs.


1 Answers

As @michael-sqlbot pointed out you can get this behavior by using the X-Amz-Invocation-Type:Event Header. Getting this setup is a little screwy and the linked documentation is accurate but a little outdated (in my opinion).

  1. The Path Override needs to be: /2015-03-31/functions/<ARN TO YOUR LAMBDA>/invocations
  2. The Execution Role needs to be able to invoke your lambda.
  3. When initially setting up the Integration Request you cannot add headers so you have to save it, then come back and add headers. From there you can hard code the X-Amz-Invocation-Type to Event by putting the string 'Event' in the Mapped from field (as pictured below)

Api Gateway Integration Request

Or... you could also achieve this by having your lambda that's wired up to the API Gateway (we'll call it lambda A) invoke another lambda (lambda B) using the Event invocation type. This way A doesn't care about the response of B and can return a successful response to the API Gateway within several hundred milliseconds (assuming you aren't doing too much else). Then Lambda B can continue running for however long is necessary (as long as it's under the 5 minute lambda limitation or your configured timeout).

like image 163
Jarred Olson Avatar answered Oct 06 '22 01:10

Jarred Olson