How do I asynchronously invoke a Lambda function from my API Gateway API?

2 minute read
1

I want to asynchronously invoke an AWS Lambda function for my Amazon API Gateway API. I don't want to synchronously invoke the function.

Resolution

REST APIs

In Lambda non-proxy integration, Lambda synchronously invokes the backend function by default. To asynchronously invoke the backend function, set the Lambda invocation type to Event.

Complete the following steps:

  1. Open the API Gateway console.
  2. Choose APIs, and then choose your REST API.
  3. In Resources, choose GET, and then choose Integration request.
  4. In Integration type, choose Lambda function.
  5. Expand URL request headers parameters, and then choose Add request header parameter.
  6. For Name, enter X-Amz-Invocation-Type.
  7. For Mapped from, enter Event.
  8. Redeploy the REST API.

To invoke the Lambda function with the option for either asynchronous or synchronous, add an InvocationType header.

Complete the following steps:

  1. Open the API Gateway console.
  2. Choose APIs, and then choose your REST API.
  3. In Resources, choose GET, and then choose Method request.
  4. In Request validator, choose the Edit icon.
  5. Choose the dropdown list, and then choose Validate query string parameters and headers.
  6. Choose the Save icon.
  7. Expand HTTP request headers, and then choose Add header.
  8. For Name, enter InvocationType, and then choose Required.
  9. In Integration Request, expand URL request headers parameters, and then choose Add request header parameter.
  10. For Name, enter X-Amz-Invocation-Type.
  11. For Mapped from, enter method.request.header.InvocationType.
  12. Redeploy the REST API.

Clients can include the InvocationType: Event header in API requests for asynchronous invocations or the InvocationType: RequestResponse header for synchronous invocations.

HTTP APIs

HTTP APIs only support proxy integrations for Lambda. You can't set the X-Amz-Invocation-Type header in the API Gateway integration for HTTP APIs. Instead, use two Lambda functions with one that acts as a proxy. To invoke one Lambda function from another, use the Invoke API and set InvocationType to Event in the first Lambda function.

Example configuration:

HTTP API invokes Lambda1 synchronously, and then Lambda1 invokes Lambda2 asynchronously.

AWS OFFICIAL
AWS OFFICIALUpdated 19 days ago
2 Comments

For http APIs how do you invoke one lambda function from another? Is that using destinations? What is the cost of passing the payload between lambda functions and does that payload also get returned to the calling function? I’m trying to decide on the best approach for processing large (100s kB) payloads in a lambda function that could take seconds to run.

replied 3 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 2 months ago