theam / aws-lambda-haskell-runtime

⚡Haskell runtime for AWS Lambda
https://theam.github.io/aws-lambda-haskell-runtime/
Other
269 stars 48 forks source link

Request never gets to my handler #105

Open seanhess opened 3 years ago

seanhess commented 3 years ago

Hi, thanks for your work on this! My lambda is invoked, but it looks like the request never gets to my handler. I'm getting an Internal Server error.

Here's a copy of the log: My haskell program is run and the wai Application is created (I'm using aws-lambda-haskell-runtime-wai). But there are no messages

FinalFive Main.hs Loaded
"Starting Lambda using API gateway handler 'handler'."
FinalFive Wai application Loaded
START RequestId: 2bb28bf8-1daf-49fe-956e-8a6d88ae72d4 Version: $LATEST
END RequestId: 2bb28bf8-1daf-49fe-956e-8a6d88ae72d4
REPORT RequestId: 2bb28bf8-1daf-49fe-956e-8a6d88ae72d4  Duration: 607.04 ms Billed Duration: 2923 ms    Memory Size: 1024 MB    Max Memory Used: 44 MB  Init Duration: 2315.74 ms
START RequestId: 3d5dc234-6c7e-400d-954a-71c4e722d666 Version: $LATEST
END RequestId: 3d5dc234-6c7e-400d-954a-71c4e722d666
REPORT RequestId: 3d5dc234-6c7e-400d-954a-71c4e722d666  Duration: 1.67 ms   Billed Duration: 2 ms   Memory Size: 1024 MB    Max Memory Used: 45 MB

The Api Gateway log says Unhandled. Does that mean an unhandled exception or a unhandled request?

{
  "requestTime":"23/Jul/2021:16:04:21 +0000",
  "requestId":"C7lE4jEHIAMEM8Q=",
  "apiId":"flj31jsfu2",
  "resourcePath":"$default",
  "path":"/ballot",
  "httpMethod":"GET",
  "stage":"$default",
  "status":"500",
  "integrationStatus":"200",
  "integrationLatency":"3308",
  "responseLatency":"3313",
  "responseLength":"35",
  "errorMessage":"Internal Server Error",
  "format":"SLS_HTTP_API_LOG",
  "version":"1.0.0",
  "integrationErrorMessage":"The Lambda function returned the following error":"Unhandled. Check your Lambda function code and try again.",
  "responseType":"INTEGRATION_FAILURE"
}

What am I missing?

seanhess commented 3 years ago

Wait, I had my Main function as a call to runWaiAsLambda, not the generate one, but now that I'm trying to switch to follow your example, I realize that your documentation doesn't match the hackage version. There is no function waiHandler. Can you update the example?

seanhess commented 3 years ago

It looks like all your documentation is out of date! There is no generateLambdaDispatcher function. The error above comes from attempting to get this to work from only the haddocks. How do you get this to work with the new version? Should I downgrade?

seanhess commented 3 years ago

The above error happened when I had the following Main.hs. How do you do this correctly with the latest version?

{-# LANGUAGE OverloadedStrings  #-}
module Lambda where

import Aws.Lambda.Wai
import Aws.Lambda (defaultDispatcherOptions)
import qualified App

main :: IO ()
main =
  runWaiAsLambda
    APIGateway
    defaultDispatcherOptions
    "handler"
    App.app -- :: IO Application
seanhess commented 3 years ago

This is when the project is configured to use the http api, not the rest api. I was able to get a basic example working simply by switching my project from http to rest.

dnikolovv commented 3 years ago

@seanhess Glad you managed to resolve the issue! The documentation is indeed lacking. Can you open an issue in https://github.com/eir-forsakring/aws-lambda-haskell-runtime-wai?

seanhess commented 3 years ago

Perhaps I didn't explain well. This issue is in this repository, it seems that it doesn't support API Gateway Http APIs (As opposed to REST. Http API requests are much cheaper).

It can be reproduced with your simple example, which doesn't use Wai at all, but configure API Gateway to use the http api in place of the rest api.

dnikolovv commented 3 years ago

I get it. We haven't had a use case for that yet, but I'll make a note to implement HTTP API support.

quyse commented 2 years ago

I initially got the same "Unhandled" error with HTTP gateway. It seems there're two supported payload formats ("1.0" and "2.0") for HTTP gateway, see AWS docs. Apparently HTTP gateway by default uses "2.0". REST gateway uses single unnamed format which seems to be the same as "1.0" format for HTTP gateway.

So, setting the format to "1.0" on AWS side seems to make it compatible with aws-lambda-haskell-runtime and fixes the issue for me. (For clarity, in Terraform I set payload_format_version = "1.0" for aws_apigatewayv2_integration resource).