Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the most recent Cloudwatch log for a certain Lambda function from the AWS CLI?

I am trying to use the AWS CLI and ASK CLI to code an Alexa skill, and I would like to be able to use the Alexa simulator and view the console logs directly from the command line to make it easy, but I am not sure how to view the last one from the command line.

I've installed the AWS and ASK CLI already and am able to view Cloudwatch logs, but there is not a fast way of view the last ones.

like image 917
Aniket Gargya Avatar asked Dec 23 '22 22:12

Aniket Gargya


1 Answers

You can use aws logs describe-log-streams to get the latest stream name and then aws logs get-log-events to get the log records themselves.

LOG_GROUP=/aws/lambda/[YOUR-LAMBDA-NAME]
LOG_STREAM=`aws logs describe-log-streams --log-group-name $LOG_GROUP --max-items 1 --order-by LastEventTime --descending --query logStreams[].logStreamName --output text | head -n 1`
aws logs get-log-events --log-group-name $LOG_GROUP --log-stream-name $LOG_STREAM --query events[].message --output text

With latest AWS CLI you can also use tail.

aws logs tail $LOG_GROUP --follow
like image 75
kichik Avatar answered May 16 '23 08:05

kichik