by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Eric Peters
    @er1c
    Hey, just looking around at aws-lambda-scala - just finished beating my head against the wall with a node.js lambda ,if I'm reading the code correctly, you have to setup the lambda handler as either a native lambda request handle or an API gateway handler, it doesn't handle both in a single API?
    Mike Kotsur
    @mkotsur
    Hey @er1c . I guess, whatever problem you had, you solved it already 😏 Apologies for not looking into this channel for so long. I’ve dropped the 🏐 on this one...
    But yes, In fact, you are right. It’s either or, API gateway events are wrapped into a thick JSON structure and the library needs to know whether it should unwrap the payload or not. We could automatically detect this at runtime, but in strongly typed languages, like Scala or Java, you still need to know at compile type which type of object are you getting passed into your handler: just the payload, or payload wrapped into the request. Does that make sense?
    Eric Peters
    @er1c
    I ended up rolling my own, not as comprehensive though, had a use case where I wanted to run it both via API gateway and also from a cloudwatch cronjob
    If I had more time, I think there would be value in abstracting out the trigger source into the same data model for the request and responses
    Vijay Emmanuel PAZHEPARAMPIL
    @vijayemmanuel
    Hello @mkotsur , I got the API gateway working for triggering the lambda function.
    This message was deleted
    Vijay Emmanuel PAZHEPARAMPIL
    @vijayemmanuel
    However, what I couldn't yet get through is how to invoke it async from the AWS API Gateway console. Following is the lambda function I am using:
    case class ShoppingItem(orderDate: String, item: String)
    object ApiGatewayScalaHandler {
      case class Req(item: String)
      case class Resp(message: ShoppingItem, request: Req)
    }  
    class ApiGatewayScalaHandler extends Proxy[Req, Resp] {
      val logger: Logger = LogManager.getLogger(getClass)
      override def handle(input: proxy.ProxyRequest[Req], c: Context): Either[Throwable, ProxyResponse[Resp]] = {
        logger.info(s"Received a request: $input")
        val headers = Map("x-custom-response-header" -> "my custom response header value")
        val responseBodyOption = input.body.map(req => Resp(ShoppingItem(DateTime.now().toString(),req.item), req))
        Right(ProxyResponse(200, Some(headers), responseBodyOption))
      }
    serverless.yml contents:
      package:
      artifact: target/scala-2.13/hello.jar
    functions:
      hello:
        handler: hello.ApiGatewayScalaHandler::handle
        events:
          - http:
              path: serverless-dev-hello-API
              method: post
              async: true
    Vijay Emmanuel PAZHEPARAMPIL
    @vijayemmanuel
    When I run it synchronously by keeping the API gateway Integration Request type to LAMBDA-PROXY, I get the following response body:
    {
      "message": {
        "orderDate": "2019-12-19T13:47:35.141Z",
        "item": "banana"
      },
      "request": {
        "item": "banana"
      }
    }
    Vijay Emmanuel PAZHEPARAMPIL
    @vijayemmanuel

    When I run it asynchronously by keeping the API gateway Integration Request type to non-proxy LAMBDA with added header X-Amz-Invocation-Type set to 'Event', I get the following content mapped to my destination Email ( I have set Email destination when lambda invokes aync and on success)

    {"version":"1.0","timestamp":"2019-12-20T12:11:44.580Z","requestContext":{"requestId":"a4299c1b-3837-4eec-a539-d6534a237037","functionArn":"arn:aws:lambda:ap-south-1:XXXXXXXXXXX:function:serverless-dev-hello:$LATEST","condition":"Success","approximateInvokeCount":1},"requestPayload":{"item":"banana"},"responseContext":{"statusCode":200,"executedVersion":"$LATEST"},"responsePayload":{"statusCode":500,"headers":{"Content-Type":"text/plain; charset=UTF-8"},"body":"Attempt to decode value on failed cursor: DownField(path)"}}

    Why do I not get the response payload same as before?

    Mike Kotsur
    @mkotsur
    @vijayemmanuel I think, this is because amazon wraps your payload into a different JSON envelope when you run the function asynchronously.
    So, Proxy[Req, Resp] won’t work for non-proxy LAMBDA integration.
    You can write your own case class, though, which will have all envelope fields and payload. If you know the structure of request, which comes in.
    Mike Kotsur
    @mkotsur
    Could you make some screenshots of your config in AWS console? I think, it would be nice to add an example of how to deal with the situation like yours to the docs.
    Vijay Emmanuel PAZHEPARAMPIL
    @vijayemmanuel
    @mkotsur : I was on X-Mas vacation so had a break.
    Vijay Emmanuel PAZHEPARAMPIL
    @vijayemmanuel
    alt
    Vijay Emmanuel PAZHEPARAMPIL
    @vijayemmanuel
    alt
    Artie Pesh-Imam
    @apeshimam
    hey is this project still active.
    Im considering using this to help write some serverless functions