8

I'm using the new HTTP Api (The one introduced in December 2019) of AWS Api Gateway.

I used to work with REST Api, which lets you trigger lambda functions async by adding X-Amz-Invocation-Type:Event header.

The new HTTP Api does not let you specify that header, how can I call lambda functions asynchronously?

thanks in advance

2 Answers 2

14

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.

In order to invoke a Lambda asynchronously via HTTP API, you can use two Lambda functions with one acting as proxy to your actual function.

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

Below is a sample NodeJS code snippet for Lambda1 to invoke Lambda2 asynchronously -

const params = {
        FunctionName: 'FUNCTION_NAME',
        InvocationType: 'Event',
        Payload: JSON.parse(event.body) // this is the event coming from API Gateway to Lambda1
    };
await lambda.invoke(params).promise(); // await here is only going to wait for the HTTP request to be successful. Once Lambda2 is invoked, it will return immediately
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer, I've read about using 2 lambda functions to make it async but I don't want to use it since it doubles the price for lambda request count. Using SQS introduces additional charges as well. The reason that I want to move from REST API to HTTP API is pricing. Do you know any other solutions to make lambda calls async without additional costs?
With HTTP APIs unfortunately no, there isn't any other option now. They currently have very limited features and were made GA only last month. Hopefully in the future AWS might come up with additional functionality.
@SurajBhatia Thanks for sharing this. How do we get the result back from the long running lambda function when not looking to store results in S3 (since pre-resigned URLs are slow)
-1

You can invoke a Lambda asynchronously with additional header Integration Request in API Gateway configuration:

For all invocations

Integration Request header in API Gateway

X-Amz-Invocation-Type : Event

To make a decision during the client call

  1. Client's request header

    InvocationType: Event or InvocationType: RequestResponse

  2. Integration Request header in API Gateway

    X-Amz-Invocation-Type header with a mapping expression of method.request.header.InvocationType.

AWS Documentation: Set up asynchronous invocation of the backend Lambda function

1 Comment

This well documented for REST API. The author explicitly mentioned they want to make this work with the HTTP API variant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.