Getting AWS Logs from the CLI

1 minute read

Lambda logs


One of the company’s developers asked me if there was any way to view the Lambda function logs from the command line, so I wouldn’t have to go into the AWS console and see them in CloudWatch Logs. After some research I got a project in Github called awslogs that does just this. Here’s how to install and use it from a Python virtual environment:

virualenv awslogs
source awslogs/bin/activate

pip install awslogs
pip install awscli

The first thing we need to know are the groups where the logs of our Lambda functions are defined, for this we use the AWS CLI:


aws logs describe-log-grups --region us-east-1
{
    "logGroups": [
        {
            "arn": "arn:aws:logs:us-east-1:123456789123:log-group:/aws/lambda/get_transcription:*", 
            "creationTime": 1531834572841, 
            "metricFilterCount": 0, 
            "logGroupName": "/aws/lambda/get_transcription", 
            "storedBytes": 0
        },
        {
            "arn": "arn:aws:logs:us-east-1:264349563434:log-group:/aws/lambda/validate_time:*", 
            "creationTime": 1532118107128, 
            "metricFilterCount": 0, 
            "logGroupName": "/aws/lambda/validate_time", 
            "storedBytes": 0
        }
    ]
}

Now to view the logs of, for example, the function get_transcription from the command line we can execute:

awslogs get  /aws/lambda/get_transcription --aws-region us-east-1  --watch  | cut -d " " -f 3-20

And the result would be something like this:

START RequestId: a2124cb1-818e-11e8-acc8-5bd968ce3fe1 Version: $LATEST
[DEBUG]	2018-07-23T15:39:50.422Z	a2124cb1-818e-11e8-acc8-5bd968ce3fe1	Searching id: e321-eaefe4311ca1
tuple indices must be integers or slices, not str: TypeError
recent call last):
File "/var/task/app/services/get_transcription.py", line 100, in lambda_handler
  date_text = field['transcription']
indices must be integers or slices, not str
END RequestId: a2124cb1-818e-11e8-acc8-5bd968ce3fe1
REPORT RequestId: a2124cb1-818e-11e8-acc8-5bd968ce3fe1	Duration: 265.66 ms	Billed Duration: 300 ms 	Memory Size: 128 MB	Max Memory Used: 33 MB

References:

Leave a Comment