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:
- Open the API Gateway console.
- Choose APIs, and then choose your REST API.
- In Resources, choose GET, and then choose Integration request.
- In Integration type, choose Lambda function.
- Expand URL request headers parameters, and then choose Add request header parameter.
- For Name, enter X-Amz-Invocation-Type.
- For Mapped from, enter Event.
- 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:
- Open the API Gateway console.
- Choose APIs, and then choose your REST API.
- In Resources, choose GET, and then choose Method request.
- In Request validator, choose the Edit icon.
- Choose the dropdown list, and then choose Validate query string parameters and headers.
- Choose the Save icon.
- Expand HTTP request headers, and then choose Add header.
- For Name, enter InvocationType, and then choose Required.
- In Integration Request, expand URL request headers parameters, and then choose Add request header parameter.
- For Name, enter X-Amz-Invocation-Type.
- For Mapped from, enter method.request.header.InvocationType.
- 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.