[英]Malformed Lambda proxy response with AWS Lambda and API Gateway NodeJs
我正在使用 API 網關和 AWS Lambda 與另一個 AWS Lambda function 交換。
我應該在初始請求插入數據后發送響應,無論是成功還是失敗。 我正在使用帶有 API 網關的特定路由。
這是結構:
現在我不斷收到 502 Malformed Lambda 代理響應,這是在我的 API 測試日志中:
Tue Sep 22 06:56:10 UTC 2020 : Endpoint response headers: {Date=Tue, 22 Sep 2020 06:56:10 GMT, Content-Type=application/json, Content-Length=4, Connection=keep-alive, x-amzn-RequestId=xxxxxxxx, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=xxxxxxx}
Tue Sep 22 06:56:10 UTC 2020 : Endpoint response body before transformations: null
Tue Sep 22 06:56:10 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response
Tue Sep 22 06:56:10 UTC 2020 : Method completed with status: 502
問題是我正在返回一個真實的響應。 正如我之前所說,我正在使用 API 網關和lambda-api a package 處理 API 網關路由,如 Express,這就是我正在做的:
api.post('/database-manager/create-request', async (lambdaRequest, lambdaResponse, callback) => {
await provideConnectionInfo()
let connection = mysql.createConnection({
host: mySQLHost,
user: mySQLUser,
password: mySQLPassword,
database: mySQLDatabase
}
)
requestNb = lambdaRequest.headers["request-id"]
pipelineId = lambdaRequest.headers["pipeline-id"]
connection.connect(function (err) {
if (err) throw err
let query = "INSERT INTO ec2_request(request_id, pipeline_id) VALUES (?,?)"
connection.query(query,[requestNb,pipelineId], function (err, result) {
if (err) {
connection.end()
console.log("insertion did not work : " + err)
let response = {
"statusCode": 404,
"headers": {},
"isBase64Encoded": false,
"body": JSON.stringify({message: "Record not inserted"})
}
return response
)
} else {
connection.end()
console.log("1 record inserted")
let response = {
"statusCode": 200,
"headers": {},
"isBase64Encoded": false,
"body": JSON.stringify({message: "tired of this bs"})
}
return response
}
}
)
}
)
})
最糟糕的是我的日志沒有顯示任何錯誤:
1600758637213 START RequestId: f804577b-c0a2-4d11-8822-52363fa41c7d Version: $LATEST
1600758639189 2020-09-22T07:10:39.188Z f804577b-c0a2-4d11-8822-52363fa41c7d INFO 1 record inserted
1600758639348 END RequestId: f804577b-c0a2-4d11-8822-52363fa41c7d
1600758639348 REPORT RequestId: f804577b-c0a2-4d11-8822-52363fa41c7d Duration: 2134.45 ms Billed Duration: 2200 ms Memory Size: 128 MB Max Memory Used: 94 MB Init Duration: 555.23 ms
如果有人看到我哪里可能犯了錯誤,我會給他們買一個甜甜圈。
想知道這是錯誤的狀態......
非常感謝,
票價
您正在async
內部使用回調 function 而無需等待。
這里有幾個選項
從async
api.post('/database-manager/create-request', async (lambdaRequest, lambdaResponse, callback) => {
刪除異步。
保持異步並將query function
包裝為Promise
並使用await
api.post('/database-manager/create-request', async (lambdaRequest, lambdaResponse, callback) => { await provideConnectionInfo() let connection = mysql.createConnection({ host: mySQLHost, user: mySQLUser, password: mySQLPassword, database: mySQLDatabase } ) requestNb = lambdaRequest.headers["request-id"] pipelineId = lambdaRequest.headers["pipeline-id"] const res = await new Promise(function(resolve, reject) { connection.connect(); let query = "INSERT INTO ec2_request(request_id, pipeline_id) VALUES (?,?)" connection.query(query,[requestNb,pipelineId], function (err, result) { if (err) { connection.end() console.log("insertion did not work: " + err) let response = { "statusCode": 404, "headers": {}, "isBase64Encoded": false, "body": JSON.stringify({message: "Record not inserted"}) } reject(response) } else { connection.end() console.log("1 record inserted") let response = { "statusCode": 200, "headers": {}, "isBase64Encoded": false, "body": JSON.stringify({message: "tired of this bs"}) } resolve(response) } }) }); return res; })
注意:代碼尚未經過測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.