1
votes

So I'm new to both python and AWS/Lambda and I'm having some trouble. I want to read cloudwatch logs using boto3 but it doesn't work. When I run the code below in AWS, I get the following error:

An error occurred during JSON serialization of response: AttributeError("'CloudWatchLogs' object has no attribute 'start_query'",) is not JSON serializable

import boto3
import time

boto_client = boto3.client('logs')


def start_query(query):
    current_time = time.time()
    response = boto_client.start_query(
        logGroupName='app-start-logging',
        startTime=current_time,
        endTime=current_time - 86400,
        queryString=query
    )
    return response

I have followed the documentation on https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/logs.html#CloudWatchLogs.Client.start_query

1
whats the output from pip freeze? I was able to reproduce your error. I setup a virtualenv and installed boto3 and your code seemed to be happy. Try virtualenv it might help pin point the problem. - al76
I cannot run the code locally as I haven't set up my machine to connect. I'm only trying to run this directly on AWS lambda - nisse33
maybe add import botocore - al76
Didn't seem to work sadly. - nisse33

1 Answers

2
votes

It appears that CloudWatch Logs Insights was introduced on November 27, 2018 (Document History - Amazon CloudWatch Logs).

The version of boto3 currently supported into AWS Lambda is 1.9.42 (AWS Lambda Runtimes - AWS Lambda).

Boto3 v1.9.42 was released on Nov 10 2018 (boto3 · PyPI).

Therefore, the version of boto3 supplied in AWS Lambda (as at the day I'm writing this) does not support CloudWatch Logs Insight.

You can upgrade the version of boto3 being used in an AWS Lambda function. See: Lambda Functions with Newer Version of boto3 than Available by Default – M&S Consulting – AWS, Salesforce, Oracle, Hadoop