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

2 minute read
1

I want to invoke an AWS Lambda function asynchronously instead of synchronously for my Amazon API Gateway API.

Resolution

REST APIs

In Lambda non-proxy integration, the backend Lambda function is invoked synchronously by default. You can configure the Lambda function for a Lambda non-proxy integration to be invoked asynchronously by specifying 'Event' as the Lambda invocation type.

1.    Open the API Gateway console, choose APIs, and then choose your REST API.

2.    In Resources, choose GET, and then choose Integration Request.

3.    In Integration type, choose Lambda Function.

4.    Expand HTTP Headers, and then choose Add header.

5.    For Name, enter X-Amz-Invocation-Type.

6.    For Mapped from, enter 'Event'.

7.    Redeploy the REST API.

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

1.    Open the API Gateway console, choose APIs, and then choose your REST API.

2.    In Resources, choose GET, and then choose Method Request.

3.    In Request Validator, choose the edit icon, choose the dropdown list, and then choose Validate query string parameters and headers.

4.    Choose the update icon to save changes.

5.    Expand HTTP Headers, and then choose Add header.

6.    For Name, enter InvocationType, and then choose Required.

7.    In Integration Request, Expand HTTP Headers, and then choose Add header.

8.    For Name, enter X-Amz-Invocation-Type.

9.     For Mapped from, enter method.request.header.InvocationType.

10.    Redeploy the REST API.

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

For more information, see Set up asynchronous invocation of the backend Lambda function.

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. You can use two Lambda functions with one acting as proxy.

Example configuration:

HTTP API --> Invoke Lambda1 synchronously --> Invoke Lambda2 asynchronously

AWS OFFICIAL
AWS OFFICIALUpdated a year ago