0

I'm using API Gateway and AWS Lambda to exchange with another AWS Lambda function.

I'm supposed to send a response after an initial request to insert data, whether it's a success or a failure. I'm using a specific route with API Gateway.

Here's the structure :

enter image description here

Now I keep getting 502 Malformed Lambda proxy response and this is in my API Test logs :

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

The problem is that I am returning a real response. As I said before, I'm using API Gateway with lambda-api a package that handles API Gateway routing like Express and this is what I'm doing :

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

                    }
                }
            )
        }
    )

})

And the worst part is my logs that don't show anything wrong :

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

If someone sees where I might've made a mistake, I'll buy them a donut.

Wondering it's the status that's wrong...

Thanks a lot in advance,

Fares

1 Answer 1

2

You are using callback function inside async without waiting.

Here are a couple of options

  1. Remove async from api.post('/database-manager/create-request', async (lambdaRequest, lambdaResponse, callback) => {.

  2. Keep async and wrap query function as Promise and use 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;   
    })
    

Note: Code has not been tested.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.