2
votes

I'm moving some scala code to AWS Lambda and I intend to have it exposed via AWS API Gateway, but I've been struggling to make the whole thing work as soon as I have one parameter.

My (very simple) code looks like this:

class HelloService {
  def hello(name: String) = {
    "hello there, " + name
  }
}

I uploaded the built jar to Lambda and tested it in the AWS console by creating a test event. It returns the right response, as expected.

However, I want this Lambda to be invoked by the API Gateway. I've used both Lambda Proxy Integration and also defined my own Body Mapping Templates. I can't seem to make it work and I keep getting:

{
  "message": "Internal server error"
}

with logs:

Execution log for request test-request Mon Jul 03 16:23:21 UTC 2017 : Starting execution for request: test-invoke-request Mon Jul 03 16:23:21 UTC 2017 : HTTP Method: GET, Resource Path: /car/aaa Mon Jul 03 16:23:21 UTC 2017 : Method request path: {carReg=aaa} Mon Jul 03 16:23:21 UTC 2017 : Method request query string: {} Mon Jul 03 16:23:21 UTC 2017 : Method request headers: {} Mon Jul 03 16:23:21 UTC 2017 : Method request body before transformations: Mon Jul 03 16:23:21 UTC 2017 : Endpoint request URI: https://lambda.eu-west-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-west-1:879461422967:function:getCarData/invocations Mon Jul 03 16:23:21 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=****************************************************************************************************************************************************************************************************************************************************************************************************************************************f8c749, X-Amz-Date=20170703T162321Z, x-amzn-apigateway-api-id=9dwaaf2mdg, X-Amz-Source-Arn=arn:aws:execute-api:eu-west-1:879461422967:9dwaaf2mdg/null/GET/car/{carReg+}, Accept=application/json, User-Agent=AmazonAPIGateway_9dwaaf2mdg, X-Amz-Security-Token=FQoDYXdzENn//////////wEaDMO73KD0CHVmggvYvSK3A8H1fpDgYiNK3HDD3ESe1aKYbv1HlGSQ85at3gRGA3kunmxVCxWbXNqR4ojBCn4hvBzdv1/iWD9xRzZQEtnQeDoO9NTuiBdYaXKgwjGozPKF/46X71f0sCt/Mm9i8EDtt3igEezJIhAF3OvYcdv2NBF3L0mRMMQKp4Vy+aC0mKu4ggadyLe+KYvmch8/AiZPlrxC1AtqwNGyWpSe1JqxeEXQGXIA5JsfwGpnpAB5IUec2r3Bd09zUFk/DCC80l9d4BLnhYAUn7xzrKYzisSEQitmhnTR3HijEYE6AJzJjFR+z2PqqVKvtgKQ [TRUNCATED] Mon Jul 03 16:23:21 UTC 2017 : Endpoint request body after transformations: { "message" : "foo" } Mon Jul 03 16:23:21 UTC 2017 : Endpoint response body before transformations: {"errorMessage":"An error occurred during JSON parsing","errorType":"java.lang.RuntimeException","stackTrace":[],"cause":{"errorMessage":"com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token\n at [Source: lambdainternal.util.NativeMemoryAsInputStream@e720b71; line: 1, column: 1]","errorType":"java.io.UncheckedIOException","stackTrace":[],"cause":{"errorMessage":"Can not deserialize instance of java.lang.String out of START_OBJECT token\n at [Source: lambdainternal.util.NativeMemoryAsInputStream@e720b71; line: 1, column: 1]","errorType":"com.fasterxml.jackson.databind.JsonMappingException","stackTrace":["com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)","com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:857)","com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java [TRUNCATED] Mon Jul 03 16:23:21 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=ede9aaed-600b-11e7-834e-47baf0a4e23f, Connection=keep-alive, Content-Length=1252, X-Amz-Function-Error=Unhandled, Date=Mon, 03 Jul 2017 16:23:20 GMT, X-Amzn-Trace-Id=root=1-595a6f79-c065d6038ba3209743378112;sampled=0, Content-Type=application/json} Mon Jul 03 16:23:21 UTC 2017 : Execution failed due to configuration error: Output mapping refers to an invalid method response: 200 Mon Jul 03 16:23:21 UTC 2017 : Method completed with status: 500

There's a null in the path of my ARN, but I guess that's because I have no authentication set, which is what's intended at this point. I don't think this would be the cause of the error.

Other than that, I've tried defining the Content-Type for the body both as application/json and text/plain. None seems to work and even with text/plain, AWS seems to be expecting json. I'd expected a string to be valid json anyway.

What am I doing wrong? What's the full expression I should put in my Body Mapping template? And how should the schema definition in my Model look like? I don't seem to be able to define a proper model for plain text.

I'm sure this is something really simple and I'm just missing something...

2

2 Answers

2
votes

The response you send back to the server depends on whether you're using the Lambda proxy integration or not. Using proxy integration is easier to set up on the API Gateway side, but your Lambda needs to do a little more work because the gateway is going to send you a bunch of stuff and demand a specific format in the response. For proxy integration the response format needs to look like this:

{
    "isBase64Encoded": true|false,
    "statusCode": httpStatusCode,
    "headers": { "headerName": "headerValue", ... },
    "body": "..."
}

There's more here: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-set-up-lambda-proxy-integration-on-proxy-resource

If you are not using proxy integration you will need to setup body mapping for your parameters in the API Gateway integration for the HTTP verb in question to match the api parameters to the lambda parameters. There's a good explanation in the accepted answer here: How to pass a querystring or route parameter to AWS Lambda from Amazon API Gateway

2
votes

With Lambda Proxy, you have to return a stringified JSON.

With Lambda, you can return JSON from your Lambda and then have your API Gateway body mapping template stringify it for you.

For additional context and example, see this page from Serverless docs.