Zio-4
Zio-4

Reputation: 269

Why does AWS Lambda function log in CloudWatch but doesn't show console.log output?

I am using CloudWatch from the AWS console and my Lambda function has a log group and log stream and I can see the recent invocations, however, the only thing that shows up in the logs is Start, End, and Report Request ID as shown below:

START RequestId: fe8ca243-288e-48f1-b585-c5d84bfb1bd7 Version: $LATEST
END RequestId: fe8ca243-288e-48f1-b585-c5d84bfb1bd7
REPORT RequestId: fe8ca243-288e-48f1-b585-c5d84bfb1bd7  Duration: 2.71 ms   Billed Duration: 3 ms   Memory Size: 128 MB Max Memory Used: 56 MB  Init Duration: 147.61 ms

Lambda Function:

var aws = require('aws-sdk');
var ddb = new aws.DynamoDB({apiVersion: '2012-10-08'});

exports.handler = async (event, context) => {
    console.log("event: ", event);

    const tableName = process.env.TABLE_NAME;
    const region = process.env.REGION;
    
    console.log("table=" + tableName + " -- region=" + region);

    aws.config.update({region: region});

    // If the required parameters are present, proceed
    if (event.request.userAttributes.sub) {

        // -- Write data to DDB
        let ddbParams = {
            Item: {
                'id': {S: event.request.userAttributes.sub},
                'username': {S: event.userName},
            },
            TableName: tableName
        };

        // Call DynamoDB
        try {
            await ddb.putItem(ddbParams).promise()
            console.log("Success");
        } catch (err) {
            console.log("Error", err);
        }

        console.log("Success: Everything executed correctly");
        context.done(null, event);

    } else {
        // Nothing to do, the user's email ID is unknown
        console.log("Error: Nothing was written to DDB or SQS");
        context.done(null, event);
    }
};

Why can't I see the console.log output?

Upvotes: 2

Views: 1916

Answers (1)

Gareth Farrington
Gareth Farrington

Reputation: 1681

This can be because the default logger's level is set to ERROR and dropped all the INFO logs. You'll need to lower that to INFO to see all of the logs in the "Test" window. At least that is what I found in Python:

import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

Upvotes: 2

Related Questions