0

我正在將一些scala代碼移動到AWS Lambda,我打算通過AWS API Gateway公開它,但我一直努力使整個工作儘快完成參數。使用AWS API網關調用AWS Lambda

我的(很簡單)的代碼如下所示:

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

我上傳內置罐子Lambda和通過創建一個測試賽測試了它在AWS控制檯。如預期的那樣,它返回正確的迴應。

但是,我希望這個Lambda可以被API網關調用。我已經使用了兩個Lambda代理集成,並且還定義了我自己的Body Mapping模板。我似乎無法使它工作,我不斷收到:

{ 
    "message": "Internal server error" 
} 

原木:

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: [email protected]; 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: [email protected]; 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

有一個在我ARN的路徑空,但我想這是因爲我沒有認證設置,這是目前的意圖。我不認爲這會是錯誤的原因。

除此之外,我已經嘗試爲應用程序/ json和text/plain定義Body-Content的內容類型。似乎沒有任何工作,甚至與文本/平原,AWS似乎期待json。無論如何,我期望一個字符串是有效的JSON。

我在做什麼錯?什麼是我應該放在我的身體映射模板中的完整表達式?我的模型中的模式定義應該如何?我似乎無法爲純文本定義合適的模型。

我敢肯定,這是很簡單的東西,我只是失去了一些東西......

回答

2

您發送回服務器的響應取決於您是否使用Lambda代理集成。使用代理集成更容易在API網關端進行設置,但您的Lambda需要做更多的工作,因爲網關將向您發送大量內容並在響應中要求特定的格式。對於代理整合的響應格式需要是這樣的:

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

還有更多在這裏: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

如果你不使用代理集成,您將需要設置體映射API網關整合您的參數用於討論中的HTTP動詞,以便將api參數與lambda參數相匹配。在這裏接受的答案有一個很好的解釋:How to pass a querystring or route parameter to AWS Lambda from Amazon API Gateway

+0

嘿,謝謝。我真的不希望我的代碼依賴於AWS​​庫,所以我選擇了Body Mapping備選方案,但您的答案是幫助我找出答案。我昨天看過那篇文章,但一定是太累了,完全錯過了解決方案。謝謝! – redwulf

1

和λ代理,你必須返回一個字符串化JSON。

使用Lambda,您可以從您的Lambda返回JSON,然後讓您的API網關正文映射模板爲您進行字符串化。請參閱this page from Serverless docs