简体   繁体   中英

Cloudwatch logs on terminal

I am using AWS Lambda for my application. For logs, I have to see in UI only, which I really don't like to see. Is there a way that I could connect to Cloudwatch logs locally and then see the logs by the tail command? Or if I could access Cloudwatch server to see logs? Basically, I wanted to see logs on my terminal. If there is any way to do that, please let me know.

Thanks for your help.

You can use AWS CLI to get your logs in real time. See: get-log-events

AWS doesn't provide a functionality where you can tail the log. There are few 3rd party tools you can use. I have used jorgebastida/awslogs which was sufficient for my needs.

Update 02/25/2021: Thanks to @adavea, I just checked and found AWS has added a new feature to tail the CW logs.

aws.logs.tail

--follow (boolean) Whether to continuously poll for new logs.

There are some command line tools like cwtail and awslogs that do a -f follow tail.

Your other option is a free tool I created called SenseLogs that does a live tail in your browser. It is 100% browser based. See https://github.com/sensedeep/senselogs/blob/master/README.md for details.

On my linux/macosx/cygwin console, this will give you the latest log file.

Substitute $1 with your group name

echo aws logs get-log-events --log-group-name /aws/lambda/$1 --log-stream-name `aws logs describe-log-streams --log-group-name /aws/lambda/$1 --max-items 1 --descending --order-by LastEventTime | grep logStreamName | cut -f2 -d: | sed 's/,//'|sed 's/\"/'\''/g'`| sh -

Note that you will need to install awscli ( https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html )

I wrapped the above in a sh function

function getcw() {
    echo aws logs get-log-events --log-group-name /aws/lambda/$1 --log-stream-name `aws logs describe-log-streams --log-group-name /aws/lambda/$1 --max-items 1 --descending --order-by LastEventTime | grep logStreamName | cut -f2 -d: | sed 's/,//'|sed 's/\"/'\''/g'`| sh -
}

and can view the latest log for my logs in chai-lambda-trigger using the command

$ getcw chai-lambda-trigger

If you wanted just the tail of the output, you could do

$ getcw chai-lambda-trigger | tail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM