10

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.

DonMorton
  • 393
  • 2
  • 11
  • 1
    In case anybody ever reads this - I still feel kind of confused, but I "think" I may be learning that if I want to use the basic Lambda Integration with Lambda Proxy, I may be out of luck on adding a header to the API Gateway to launch asynchronously. This may explain why - https://medium.com/@lakshmanLD/lambda-proxy-vs-lambda-integration-in-aws-api-gateway-3a9397af0e6d – DonMorton Sep 23 '18 at 00:44

3 Answers3

10

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.

Sarah Fnc
  • 101
  • 1
  • 7
  • Where is that, looked all over the API Gateway can see that anywhere – jpw Mar 30 '21 at 05:28
  • Click the method where you want to define header. a page will come for testing that method, look out the square box with name Integration Request , here you should find this. – Faisal Shani Jan 07 '22 at 12:04
6

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:

  1. In the API Gateway console under your Method Request settings under HTTP Request Headers add a InvocationType header.
  2. In your Integration Request settings create a X-Amz-Invocation-Type HTTP Header as you have indicated.
  3. 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 the InvocationType: Event header 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).

Devin Cairns
  • 650
  • 6
  • 9
  • but where is "method Request" settings? – jpw Mar 30 '21 at 05:57
  • AWS Console -> API Gateway -> [your api name] -> method (POST, GET, etc.) -> Method Request – Devin Cairns Mar 31 '21 at 15:31
  • I've lost a couple of days trying to get this to work before I came across your solution. I'm still not entirely sure what's going on but my payload now makes its way onto an SQS queue from API Gateway, thank you very much for your solution. – mister_b Aug 11 '21 at 17:02
2

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.

api-gateway-rest-api

If you created an API Gateway using HTTP API, your interface would instead look like this:

api-gateway-http-api

  • Is it possibile to do what the OP asks using an HTTP API? EDIT: answering myself, no it's not possible. see https://stackoverflow.com/a/61083679/4178025 – Diego Aug 23 '23 at 00:24