Prerna Kakria
Prerna Kakria

Reputation: 87

AWS - Lambda function not waiting for async/await axios call

I have a lambda function in which I am making an axios call to a third party API. I have put my post request in try/catch and also using async await but nothing is getting printed in cloudwatch logs. The lambda function is not timeouting, I have even set my lambda timeout time to 15 minutes. It seems like the axios call is never invoking. Below is my code:

exports.saveWebhooks = async function (req, res) {
    try {
        const data = req.body
        let insertData = {
            insertedAt: moment().format('YYYY-MM-DD HH:mm:ss'),
            data: data,
            status: 0
        }
        await db.collection('gupshup_interactive_webhooks').insertOne(insertData);
        res.status(200).send('');
        console.log('inserted');
        const payload = data.payload;
        const userMessage = payload.payload.text;
        if (data.type == 'message') {
            if (userMessage == 'hey' || userMessage == 'Hey') {
                console.log("in hey");
            } else {
                // console.log("in else");
                const param = {
                    message: '{"type":"text","text":"Sorry, I didn\'t get you. Type \"Hey\" to start!"}',
                    channel: `your text`'whatsapp',
                    source: 91xxxxxxxxx,
                    'src.name': 'Demo',
                    destination: 91xxxxxxxxx
                };
                const config = {
                    method: 'post',
                    url: 'https://third_party_api',
                    headers: {
                        'accept': 'application/json',
                        'Content-Type': 'application/x-www-form-urlencoded',
                        'apikey': 'xxxxxxxxxxxx'
                    },
                    data: param
                };
                try {
                    console.log(config)
                    const response = await axios(config);
                    console.log(response.data);
                } catch (err) {
                    console.log(err.message)
                    console.log(err.response.data)
                }
                console.log("after axios call")
            }
        } else {
            // Handle other cases if needed
        }
    } catch (error) {
        console.error(error);
    }
}

In my above code saveWebhooks is my endpoint in which I have attached API Gateway. The code is running fine and all console.logs are getting printed even the console that says after axios call is also getting printed but nothing is printed inside try/catch for axios call.

Also, the code is running fine in local. The third party API which I am using is to send a whatsapp message using gupshup API's. My lambda function is not running in VPC and I have also given the AWSLambdaVPCAccessExecutionRole permission to my role so that lambda can access the internet.

Can anybody please help on what I might be missing in my code?

Upvotes: 1

Views: 360

Answers (1)

Andreas
Andreas

Reputation: 2366

The below line will send the response and exit the middleware/code.

res.status(200).send('');

Upvotes: 0

Related Questions