【问题标题】:boto3 client cannot serialize correctlyboto3 客户端无法正确序列化
【发布时间】:2019-05-07 10:46:30
【问题描述】:

所以我是 python 和 AWS/Lambda 的新手,我遇到了一些麻烦。我想使用 boto3 读取 cloudwatch 日志,但它不起作用。当我在 AWS 中运行以下代码时,出现以下错误:

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

我已关注https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/logs.html#CloudWatchLogs.Client.start_query 上的文档

【问题讨论】:

  • pip freeze 的输出是什么?我能够重现您的错误。我设置了一个 virtualenv 并安装了 boto3,你的代码似乎很开心。尝试 virtualenv 它可能有助于查明问题。
  • 我无法在本地运行代码,因为我还没有设置我的机器进行连接。我只是想直接在 AWS lambda 上运行它
  • 可以加import botocore
  • 可悲的是似乎没有工作。

标签: python-3.x amazon-web-services aws-lambda boto3


【解决方案1】:

CloudWatch Logs Insights 似乎于 2018 年 11 月 27 日推出 (Document History - Amazon CloudWatch Logs)。

AWS Lambda 目前支持的boto3 版本是1.9.42 (AWS Lambda Runtimes - AWS Lambda)。

Boto3 v1.9.42 于 2018 年 11 月 10 日发布 (boto3 · PyPI)。

因此,AWS Lambda 中提供的 boto3 版本(截至我撰写本文时)不支持 CloudWatch Logs Insight

您可以升级在 AWS Lambda 函数中使用的 boto3 版本。见:Lambda Functions with Newer Version of boto3 than Available by Default – M&S Consulting – AWS, Salesforce, Oracle, Hadoop

【讨论】:

  • 嗯,有趣的是,当我打印 boto4 的版本时,看起来我有 boto3 版本:1.7.74 botocore 版本:1.10.74
  • 在您自己的计算机上?然后你应该用pip install boto3 --upgrade更新。
  • 不,在 aws... 我尝试做博客中描述的事情。它确实有效,但现在我无法内联编辑代码,因为该项目有 50MB 大......在我的本地机器上,我得到了 1.9.142
  • 为什么我在亚马逊上的boto3版本超级旧?我删除了我添加的 boto3 包并尝试使用 python 3.7 运行,因为它应该支持更新的 boto3 包:docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html 但它产生的结果与我之前的相同。
  • 我不确定您所说的“在亚马逊上”是什么意思,但如果您的意思是在 Amazon EC2 实例上,那么它将取决于用于启动该实例的 AMI。如上所述,您可以升级版本。 python2 和 python3 之间也可能会混淆,因此您可能需要使用pip3。为 python 创建一个 Virtual Env 以避免版本冲突总是最安全的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多