我正在使用API Gateway和AWS Lambda与另一个AWS Lambda函数进行交换。
我应该在初始请求插入数据后发送响应,无论是成功还是失败。我正在通过API网关使用特定的路由。
这是结构:
现在,我不断收到502格式错误的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 Gateway与lambda-api一起使用,该软件包可以处理诸如Express之类的API Gateway路由,这就是我正在做的事情:
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
如果有人发现我可能在哪里做错了,我会给他们买一个甜甜圈。
想知道这是错误的状态...
非常感谢
票价
答案 0 :(得分:2)
您正在async
内部使用回调函数,而无需等待。
这里有几个选择
从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;
})
注意:代码未经测试。