Working with logs - AWS Serverless Application Model

Working with logs

To simplify troubleshooting, the AWS SAM CLI has a command called sam logs. This command lets you fetch logs generated by your Lambda function from the command line.


The sam logs command works for all AWS Lambda functions, not just the ones you deploy using AWS SAM.

Fetching logs by AWS CloudFormation stack

When your function is a part of an AWS CloudFormation stack, you can fetch logs by using the function's logical ID:

sam logs -n HelloWorldFunction --stack-name mystack

Fetching logs by Lambda function name

Or, you can fetch logs by using the function's name:

sam logs -n mystack-HelloWorldFunction-1FJ8PD

Tailing logs

Add the --tail option to wait for new logs and see them as they arrive. This is helpful during deployment or when you're troubleshooting a production issue.

sam logs -n HelloWorldFunction --stack-name mystack --tail

Viewing logs for a specific time range

You can view logs for a specific time range by using the -s and -e options:

sam logs -n HelloWorldFunction --stack-name mystack -s '10min ago' -e '2min ago'

Filtering logs

Use the --filter option to quickly find logs that match terms, phrases, or values in your log events:

sam logs -n HelloWorldFunction --stack-name mystack --filter "error"

In the output, the AWS SAM CLI underlines all occurrences of the word "error" so you can easily locate the filter keyword within the log output.

Error highlighting

When your Lambda function crashes or times out, the AWS SAM CLI highlights the timeout message in red. This helps you easily locate specific executions that are timing out within a giant stream of log output.

JSON pretty printing

If your log messages print JSON strings, the AWS SAM CLI automatically pretty prints the JSON to help you visually parse and understand the JSON.