Your Lambda function can contain logging statements. AWS Lambda writes these logs to CloudWatch. If you use the Lambda console to invoke your Lambda function, the console displays the same logs.
The following Node.js statements generate log entries:
console.log()
console.error()
console.warn()
console.info()
For example, consider the following Node.js code examples:
-
The first sample can be written using either runtime version 6.10 or 4.3.
console.log('Loading function'); exports.handler = function(event, context, callback) { //console.log('Received event:', JSON.stringify(event, null, 2)); console.log('value1 =', event.key1); console.log('value2 =', event.key2); console.log('value3 =', event.key3); callback(null, event.key1); // Echo back the first key value };
-
The second sample uses the Node.js
async
feature, available only in runtime versions 8.10 or later.console.log('Loading function'); exports.handler = async function(event) { //console.log('Received event:', JSON.stringify(event, null, 2)); console.log('value1 =', event.key1); console.log('value2 =', event.key2); console.log('value3 =', event.key3); return event.key1 // Echo back the first key value };
In either case, the following screenshot shows an example Log output section in the Lambda console. You can examine the same information in CloudWatch Logs. For more information, see Accessing Amazon CloudWatch Logs for AWS Lambda.
The console uses the RequestResponse
invocation type (synchronous invocation) when invoking the function, therefore it gets the return value (value1
) back from AWS Lambda which the console displays.
To test the preceding Node.js code in AWS Lambda console
-
In the console, create a Lambda function using the hello-world blueprint. Make sure to select the Node.js as the runtime. For instructions on how to do this, see Create a Lambda Function with the Console.
-
Test the Lambda function using the Sample event template called Hello World provided in the Lambda console. You can also update the code and try other logging methods and properties discussed in this section.
For step-by-step instructions, see Getting Started.
You can find the logs that your Lambda function writes, as follows:
-
In the AWS Lambda console – The ** Log output** section in the AWS Lambda console shows the logs.
-
In the response header, when you invoke a Lambda function programmatically – If you invoke a Lambda function programmatically, you can add the
LogType
parameter to retrieve the last 4 KB of log data that is written to CloudWatch Logs. AWS Lambda returns this log information in thex-amz-log-results
header in the response. For more information, see Invoke.If you use AWS CLI to invoke the function, you can specify the
--log-type parameter
with valueTail
to retrieve the same information. -
In CloudWatch Logs – To find your logs in CloudWatch you need to know the log group name and log stream name. You can get that information by adding the
context.logGroupName
, andcontext.logStreamName
methods in your code. When you run your Lambda function, the resulting logs in the console or CLI will show you the log group name and log stream name.