Node.js AWS Lambda和API网关节点的Lambda代理响应格式错误

Node.js AWS Lambda和API网关节点的Lambda代理响应格式错误,node.js,amazon-web-services,aws-lambda,aws-api-gateway,Node.js,Amazon Web Services,Aws Lambda,Aws Api Gateway,我正在使用API网关和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=applicat

我正在使用API网关和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网关和一个处理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
中使用回调函数,无需等待

这里有几个选择

  • api.post('/database manager/create request',async(lambdaRequest、lambdaResponse、callback)=>中删除
    async

  • 保持异步并将
    查询功能
    包装为
    承诺
    并使用
    等待

    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;   
    })
    

  • 注意:代码尚未测试。

    您在async中使用回调函数,无需等待

    这里有几个选择

  • api.post('/database manager/create request',async(lambdaRequest、lambdaResponse、callback)=>中删除
    async

  • 保持异步并将
    查询功能
    包装为
    承诺
    并使用
    等待

    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;   
    })
    
  • 注意:代码尚未测试