1

Below is the lambda function:

exports.lendingLambdaHandler = async (event, context) => {
    const records = event.Records

    console.log(records)

    return {}
};

executed with NodeJS runtime ( as serverless app) that writes the incoming event record to stdout

By definition, console.log prints to stdout, as mentioned here, but I need to go through cloud watch logs, as this is AWS serverless app:

enter image description here


What is the correct way to view stdout in AWS environment?

jww
  • 97,681
  • 90
  • 411
  • 885
overexchange
  • 15,768
  • 30
  • 152
  • 347
  • Have you tried ‘context.log’? In Azure, that allows you to see logs from their console portal. – westandy Jul 13 '19 at 20:06
  • Also see [Can bash script be written inside a AWS Lambda function](https://stackoverflow.com/q/34629574/608639), [Invoking aws lambda without output file](https://stackoverflow.com/q/47675032/608639), etc. – jww Jul 13 '19 at 21:31

3 Answers3

2

This is the standard way for handling logging in AWS Lambda. A quick glance at the docs show there's no extra configuration options available.

That said, there are a few things that might work better for you.

Viewing logs in your terminal:

aws lambda invoke --function-name YOUR_FUNCTION out --log-type Tail

Streaming Cloudwatch logs to another service: Link to article

Using the streaming method gives you a lot more control, and let's you send your logs to other services, or customize the logging format and save that anywhere you want.

refs:

AWS Lambda Docs

HMilbradt
  • 3,980
  • 16
  • 31
  • What is `console.log` doing in AWS serverless app? what does it mean to write to `stdout`? if it is a serverless app... – overexchange Jul 13 '19 at 19:59
  • 1
    stdout is just a place to send output, it doesn't have to be a file or console.. In this case, it's being sent directly to Cloudwatch. – HMilbradt Jul 13 '19 at 20:16
  • `stdout` is a second entry of file descriptor table,in [process control block](https://en.wikipedia.org/wiki/Process_control_block) – overexchange Jul 13 '19 at 20:27
  • @overexchange the Lambda runtime environment for Node.js captures both stdout and stderr, and anything your code writes to either of those file handles is sent to CloudWatch Logs. The first ~4KB of output is also optionally returned in the response to the Invoke request. Those are the only places it goes: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-logging.html Using `console.log()` is simply one way of writing to stdout, with formatting: https://stackoverflow.com/a/4984464/1695906 – Michael - sqlbot Jul 13 '19 at 23:06
2

Lambda function is serverless right, but that doesn't mean that there is no server running your lambda function, all it really means is that you don't see that server and don't have to manage it at all.

When you invoke lambda function, there is a fleet of servers waiting to launch a new docker container in which your lambda function is going to be executed (or reuse existing execution context if your lambda function is being invoked constantly), the bottom line is that stdout in case of lambda is the same stdout as if you have run the code locally (or on a server which you have access to), the difference is that you don't have access to that underlying docker container where you would observe it. Hence the logs are also being sent to CW Logs so that you have a way of accessing them.

What is the correct way to view stdout in AWS environment?

As I have mentioned, you don't have access to the underlying container in which your lambda function is being executed so the correct way of accessing logs is through CW Logs. Of course, you may choose to stream the logs from CW to any other service as you like or as your use case requires.

Matus Dubrava
  • 13,637
  • 2
  • 38
  • 54
0

This should work as you don't have to create an output file and read the logs directly from stdout:

aws lambda invoke --function-name LAMBDA_FUNCTION_NAME \
                  --log-type Tail /dev/null \
                  --query 'LogResult' \
                  --output text | base64 -d

In case you need to specify a profile name:

aws lambda invoke --function-name LAMBDA_FUNCTION_NAME \
                  --log-type Tail /dev/null \
                  --query 'LogResult' \
                  --output text \
                  --profile PROFILE_NAME | base64 -d
0xack13
  • 462
  • 4
  • 8