'Trying to add X-Amz-Invocation-Type:Event to existing API Gateway POST method
First, the basic question - is there a straightforward way to add the HTTP header X-Amz-Invocation-Type:'Event' to an existing API Gateway POST method with Integration Type Lambda Function so that I can easily launch an asynchronous Lambda from my existing endpoint?
Now the background...
I have an API Gateway with a POST method, Integration type Lambda Function that works quite well for me via curl and Javascript, except for the 29s API timeout.
Through some searching, I think I've learned that setting up an HTTP header X-Amz-Invocation-Type:'Event' is what I would need to get my Lambda function to run asynchronously.
I've seen advice that suggests going through the console and using an AWS Service integration type, then adding in the header along with mapping templates and such, and this is far more complicated than I really want to try (maybe I don't have a choice?). I tried, but I "think" I end up having to deal with mapping templates, and I have a pretty complicated payload - does it really have to be this hard?
I've also seen suggestions to use two Lambdas, the first one is triggered through the API and is synchronous, and triggers the asynchronous one. I know how to do this, and it does have advantages (but also added complexity), but I'm putting that aside for now.
I read one post that "hinted" that it might be possible to add this new HTTP header via CLI. I've done a bit of trial and error with things like aws.apigateway.put_method() etc., but I'm kind of shooting in the dark. I like to think there's an easy way to do this, but the solution is evading me up to now.
Solution 1:[1]
I've faced this isssue also here is how I found the Header should be define: in the panel "Integration Request" of your POST method: screenshot here
The single quotes around 'Event' are important, so I've read (as it is a static value). With this set up, you can test your method and you will receive an instant (well ~100ms after) 200 code.
Solution 2:[2]
I was struggling with the same issue, and turns out there are multiple ways to make this work. Lambda is run synchronously or asynchronously depending on resources that invoke it. Using API Gateway, you can customize this behaviour using the InvocationType header. There are two ways to use this header:
- In the API Gateway console under your Method Request settings under HTTP Request Headers add a
InvocationTypeheader. - In your Integration Request settings create a
X-Amz-Invocation-TypeHTTP Header as you have indicated. In the "Mapped From" setting you have two options:
If you would like to selectively run synch. or asynch. from your API call you can use the value
method.request.header.InvocationType. When making calls to your method use theInvocationType: Eventheader if you want the function the run asynch.To always invoke asynchronously, specify the value
'Event'(important to include the single quotes).
One other thing to note- if invoking your function asynchronously, you will get a no data response (obviously).
Solution 3:[3]
One thing that I think people fail to mention is that the "Method" tab is only available if you use REST API and not if you use HTTP API for your API Gateway. This was not clear on the documentation. See my screenshot for what the REST API option looks like and where the Invocation Request is.
If you created an API Gateway using HTTP API, your interface would instead look like this:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Devin Cairns |
| Solution 3 |


