mkotsur / aws-lambda-scala

Writing AWS Lambdas in Scala

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

problem with aws-lambda-scala + SAM local

supermanue opened this issue · comments

Hi Mkotsur,

I am trying to implement a lambda function with your library and test it with SAM local, but I cannot make it work.

I am quite newbie in this lambda stuff, so I am surely making something very obvious wrong. However I cannot find the problem, so I was hoping that maybe you could point me to the solution.

I have implemented functions to be accessed directly and through the ApiGateway. None of them works, but I am not sure of which one should I be using.

This is all my code and configuration. Maybe it is too much information, but hopefully this clearly states the problem.

Thanks for your help,

Manuel


source:

package user.handler

import io.circe.generic.auto._
import io.github.mkotsur.aws.handler.Lambda._
import io.github.mkotsur.aws.handler.Lambda
import com.amazonaws.services.lambda.runtime.Context
import io.github.mkotsur.aws.proxy
import io.github.mkotsur.aws.proxy.ProxyResponse

case class Ping(inputMsg: String)
case class Pong(outputMsg: String)

class PingPong extends Lambda[Ping, Pong] {
override def handle(ping: Ping, context: Context) = Right(Pong(ping.inputMsg.reverse))
}

class ApiGatewayPingPong extends Proxy[Ping, Pong] {
override def handle(input: proxy.ProxyRequest[Ping], c: Context): Either[Throwable, ProxyResponse[Pong]] = {
val headers = Map("x-custom-response-header" -> "my custom response header value")

val responseBodyOption = input.body.map(ping => Pong(ping.inputMsg.reverse) )
Right(ProxyResponse(200, Some(headers), responseBodyOption))
}
}

configuration:
(note that here I have both used "PingPong" and "ApiGatewayPingPong" for the handler and both fail with the same error)

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Globals:
Function:
Timeout: 300

Resources:

HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./target/scala-2.12/hello.jar
Handler: user.handler.ApiGatewayPingPong
Runtime: java8
Events:
pingpong:
Type: Api
Properties:
Path: /pingpong
Method: post

compilation:
sbt assembly

execution:
sam local start-api

test: POST.
curl --request POST --url http://127.0.0.1:3000/pingpong --data '{ "inputMsg": "hello" }'

output (client):
{"message":"Internal server error"}

output (server):

running:

2019-02-17 10:37:55 Found credentials in shared credentials file: ~/.aws/credentials
2019-02-17 10:37:55 Mounting HelloWorldFunction at http://127.0.0.1:3000/pingpong [POST]
2019-02-17 10:37:55 You can now browse (...)
2019-02-17 10:37:55 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)

after post:

2019-02-17 10:38:12 Invoking user.handler.ApiGatewayPingPong (java8)
2019-02-17 10:38:12 Decompressing (...)scala-2.12/hello.jar
Fetching lambci/lambda:java8 Docker container image......
2019-02-17 10:40:35 Mounting (...)/tmpggbc_kp1 as /var/task:ro inside runtime container
START RequestId: e6765fde-5d14-4d3c-9332-8c63a91ff293 Version: $LATEST
null <-------- this is the error
END RequestId: e6765fde-5d14-4d3c-9332-8c63a91ff293
REPORT RequestId: e6765fde-5d14-4d3c-9332-8c63a91ff293 Duration: 40.08 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 5 MB
2019-02-17 10:40:38 Function returned an invalid response (must include one of: body, headers or statusCode in the response object). Response received:
2019-02-17 10:40:38 127.0.0.1 - - [17/Feb/2019 10:40:38] "POST /pingpong HTTP/1.1" 502 -

Hi @supermanue. I've been looking into this today and could reproduce the issue. I don't know what's going on, but I tend to suspect sam local in calling the function in some strange way. Will look further tomorrow and let you know.

Hey @supermanue,
So, I've looked the day after, but was tired and went the way of debugging everything and everywhere, which didn't give me much 😞

Now that I've taken a fresh look, I think, there is one 100% mistake in the template you're trying to run the lambda with. The reference to the handler should also include the method name. E.g.:

Handler: user.handler.ApiGatewayPingPong::handle

Could you try to fix that and let me know what happens 🤔?

Ok, I've doublechecked, and this seems indeed to be the cause of the problem. SAM could have had a better error message for that case 😐

Closing the issue, for now. Let me know if there is still something wrong.

sorry for the delay. It does work like a charm. Great library, thanks!