承上文 [Java] AWS Lambda 入門, 這次要在前面掛上 API Gateway。
首先設定好 API Gateway 串到之前的 Lambda,測試的時候會出現 Status 502 的狀況,
Execution failed due to configuration error: Malformed Lambda proxy response主要原因是 Lambda 回傳給 API Gateway 有特定格式,以下用 Node 當作範例:
exports.handler = (event, context, callback) => { var response = { "statusCode": 200, //可略,默認為200 "headers": { //可略 "my_header": "my_value" }, "body": JSON.stringify(event), "isBase64Encoded": false //可略,默認為非 }; callback(null, response); };也就是說需要回傳 Json 物件,同時把資料放在 "body" 裡面,API Gateway 才會顯示正常。 在 Lambda 上測試的結果要像這樣:
{ "statusCode": 200, "headers": { "my_header": "my_value" }, "body": "{\"key3\":\"value3\",\"key2\":\"value2\",\"key1\":\"value1\"}", "isBase64Encoded": false }Java 的回傳格式也必須相同。
參考資料:
- Pixnet: API Gateway
- AWS Support: I received a "malformed Lambda proxy response" error or a 502 status code with Amazon API Gateway. How do I resolve this?
系列:
- [Java] AWS Lambda 入門
- [Java] AWS Lambda + DynamoDB
- AWS Lambda + API Gateway
- [Java] JAX-RS on AWS Lambda
- [Java] JUnit with DynamoDBLocal
Thank you for your guide to with upgrade information about AWS keep update at
回覆刪除AWS
Online Course