View your AWS CLI logs in Real Time (tail)

avatar
Borislav Hadzhiev

Last updated: Feb 26, 2024
2 min

banner

# How to tail your AWS CLI logs

To tail your CloudWatch logs in real time, add the --follow parameter to the aws logs tail command. The command tails the logs for a specific CloudWatch log group.

By setting the --follow parameter, the command continuously polls for new logs.

The following example shows how to tail the logs of a lambda function using the AWS CLI.

shell
aws logs tail "/aws/lambda/your-lambda-name" --follow

tail aws lambda logs

The syntax of the command looks as follows.

shell
aws logs tail "YOUR_LOG_GROUP_NAME" --follow

By default, the logs tail command prints the:

  • timestamp and timezone.
  • log stream name.
  • log messages.

For a more readable version, set the --format parameter to short.

shell
aws logs tail "/aws/lambda/your-lambda-name" --follow --format short

aws cli tail logs human readable

The short version prints only the timestamp and the log messages.

The aws logs tail command also enables us to view the generated logs of a CloudWatch log group for a specific time period.

By default the command returns the logs from the past 10 minutes.

To return the logs from a specific time period, use the --since parameter:

shell
# Display the logs from the past 30 minutes aws logs tail "/aws/lambda/your-lambda-name" --follow --since 30m # Display the logs from the past 10 seconds aws logs tail "/aws/lambda/your-lambda-name" --follow --since 10s # Display the logs from the past 3 hours aws logs tail "/aws/lambda/your-lambda-name" --follow --since 3h # Display the logs from the past 2 days aws logs tail "/aws/lambda/your-lambda-name" --follow --since 2d # Display the logs from the past 3 weeks aws logs tail "/aws/lambda/your-lambda-name" --follow --since 3w

You can filter which log messages the aws logs tail command displays to your terminal, by using the --filter-pattern parameter.

The following example only returns log messages that include the string Hello.

shell
aws logs tail "/aws/lambda/your-lambda-name" --follow --format short --filter-pattern "Hello"

aws cli tail simple filter

The following example returns log messages that include the string Hello OR the string END.

shell
aws logs tail "/aws/lambda/your-lambda-name" --follow --format short --filter-pattern "?Hello ?END"

aws cli tail or filter

To filter for multiple strings, simply prefix them with a ? character.

You can read more about the filter syntax for the aws logs tail command by going to the docs.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.