Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda call is not logging in console

I am learning Lambda and trying to migrate my local app to a AWS lambda. It is very simple. Just a function which calls a external service. My handler method call this method:

export const addTrack = async (token: string, parameters: any) => {
    const url: string = process.env.URL as string
    const headers = { ...globalHeaders, "authorization": "Bearer " + token };
    try {
        console.log("PARAMS:", parameters);
        let response = await axios.put(url, parameters, { headers })        
        console.log('RESPONSE:', response.status + " | " + response.statusText + " | " + parameters.date + " | " + parameters.hours + " | " + parameters.comments)
    } catch (e) {
        console.error('Error trying to send a record:', parameters)
        console.error(e)
        throw new Error('Failing to call API: ' + e)
    }
}

handler method:

import { addTrack } from "./time-tracker-api-client"
import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb"

const dynamoClient = new DynamoDBClient({})

export const handler = async (message: any) => {    
    let body = JSON.parse(message.Records[0].body);    

    let { Item }:any = await //call to dynamoBD...
    let token:string = Item.bdToken.S
    addTrack(token, body)
}

Note the console.log before and after axios.put call and a log in the catch block. I am having some intermitent logs here. Sometimes it shows, sometimes not. The log with PARAMS always shows, so it is correct. But sometimes the RESPONSE log does not show in cloudwatch logs in any streams, but also does not show any error log. It seems the put call is causing some error, but it do not enter in the catch block. The weird thing is sometimes when error happens in tha put call, it shows in the log.

For example, lambda is called 20 times, I have 20 PARAMS log in streams, My external services respond to 17 calls. I have only 17 RESPONSE logs and 1 errors logs. Seems 2 error logs is missing. Sometimes the external call works, but the RESPONSE log does not appear

I do not understand what is happening here. Can someone have some tips in what is happening?

I have tried to call using the .then().catch() in put call, but it does not work also.

like image 681
M. Brandao Avatar asked Jul 22 '26 18:07

M. Brandao


1 Answers

It seems that you are getting spontaneous errors (3 out of 20) because your Lambda function's code handler is not awaiting the addTrack function to complete before terminating. To ensure that addTrack function is completely executing, change your handler code to the following:

import { addTrack } from "./time-tracker-api-client"
import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb"

const dynamoClient = new DynamoDBClient({})

export const handler = async (message: any) => {    
    let body = JSON.parse(message.Records[0].body);    

    let { Item }:any = await //call to dynamoBD...
    let token:string = Item.bdToken.S

    // Gracefully await the addTrack function to finish.
    await addTrack(token, body);
}
like image 82
Allan Chua Avatar answered Jul 24 '26 16:07

Allan Chua



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!