2

I have a AWS Gateway API that defines a simple GET. I use that to trigger a Lambda. Using the request mapping i take the query params and create a json object to give the Lambda. Here is my mapping

#set ($myMap = $input.params().get("querystring"))

{
    #foreach($paramName in $myMap.keySet() )
    "$paramName" : "$util.escapeJavaScript($myMap.get($paramName))"
        #if($foreach.hasNext),#end
    #end
}

I am new to VTL and Amazon Gateway API and it seems i am making a mistake that is cause this to never complete. I have tried many variations but if i ever quote $myMap.get($key) it wont complete. What am i doing wrong? Id love to understand. Suggestion for same output also welcome.

UPDATE

If i remove the "{" and "}" this no longer times out! Now i dont know why that matters and it is not the format i want at all but interesting tidbit to get to the bottom of this.

Update 2

Simplified the logic and its reflected above with the same issue. I also found that if put an attribute in the json that opens another object the logic works. This is terrible.

{

    "data": {
    #foreach($paramName in $myMap.keySet() )
    "$paramName" : "$util.escapeJavaScript($myMap.get($paramName))"
        #if($foreach.hasNext),#end
    #end
    }
}
Hank D
  • 6,271
  • 2
  • 26
  • 35
Nexeh
  • 213
  • 3
  • 18

3 Answers3

1

The first mapping template (pasted below) you provided in your question works fine.

#set ($myMap = $input.params().get("querystring"))

{
    #foreach($paramName in $myMap.keySet() )
        "$paramName" : "$util.escapeJavaScript($myMap.get($paramName))"
        #if($foreach.hasNext),#end
    #end
}

If you are still seeing timeout with that mapping, we can analyze more. You can use API Gateway forum and send private message with your API and call details.

The TestInvoke feature in API Gateway Console has a time limit of 10 seconds. Since the Lambda function may have a cold start, sometimes it may take longer. So, in actual invoke (after deploying the API), we increased the time limit to 30 seconds for calls that hit Lambda cold start.

Balaji
  • 1,028
  • 8
  • 12
  • Thanks for your response. You are correct.I posted the same results in an answer as well. SO just didn't let me accept it yet. I hope AWS can get better messaging to prevent this confusion in the future. I am curious how you tested the VTL. This is the first time i have used it and only because it was a part of API Gateway. – Nexeh Apr 04 '16 at 14:08
0

Unfortunately, you doing everything right.

As of today AWS API Gateway has a very hard limit that it times out if request takes longer then 10 seconds.

Here is the official AWS thread that you can follow for updates: https://forums.aws.amazon.com/thread.jspa?threadID=205424

Vor
  • 33,215
  • 43
  • 135
  • 193
  • Thanks for your response Vor! I understand there would be a limit to execution time. that completely makes sense! What doesn't make sense is why this would cause it to run for more than 10 seconds. This runs sub second without attempting to add a quotation. I think there is a developer issue here, that being me, not understanding some part of the VTL syntax Or a VTL/AWXS issue where it cant handle this case. But i am perplexed about the problem. – Nexeh Apr 01 '16 at 15:01
0

As Vor stated above, the gateway api has a limit of 10 seconds. In my case what was happening was that when i got the format right my lambda would execute without throwing an exception. This logic was taking longer than the 10 second. What made this situation REALLY confusing was AWS output when testing the REST API. In most cases you get a page with three sections "Response Body", "Headers", and "log". You look at this log section to see how your VTL template did. You can compare the input and the output and the invocation to Lambda. Great for debugging.

In the case of timing out the log section doesn't get populated. Just the response body that i posted in the original question. The Lambda seemingly didn't create any logs in CloudWatch either. This gave the impression that an error occurred in the setup of the API and the request mapping that was fatal enough that it didnt even Invoke the Lambda.

Another interesting finding is that this no longer timed out after i tested the lambda directly the first time. By essentially "priming the pump" subsequent lambda requests responded in half the time. After the prime i can use the Gateway API just fine.

Nexeh
  • 213
  • 3
  • 18