【问题标题】:I am trying to setup an AWS lambda that will start an RDS Reboot. Here is my lambda function:我正在尝试设置一个将启动 RDS 重启的 AWS lambda。这是我的 lambda 函数:
【发布时间】:2022-01-11 07:11:53
【问题描述】:
import sys
import botocore
import boto3
from botocore.exceptions import ClientError

def lambda_handler(event, context):
    rds = boto3.client('rds')
    lambdaFunc = boto3.client('lambda')

    print ('Trying to get Environment variable')
    try:
        funcResponse = lambdaFunc.get_function_configuration(
        FunctionName='RDSInstanceReboot'
        )

        DBinstance = funcResponse['Environment']['Variables']['DBInstanceName']
        print ('Stoping RDS service for DBInstance : ' + DBinstance)
        
    except ClientError as e:
        print(e)    
    try:
                response = rds.reboot_db_instance(
                 DBInstanceIdentifier=DBinstance
                )
                print ('Success :: ')
                return response
    except ClientError as e:
        print(e)    
    return
{
'message' : "Script execution completed. See Cloudwatch logs for complete output"
}

但是,当我运行时,我得到以下输出:

[ERROR] Runtime.MarshalError: Unable to marshal response: datetime.datetime(2021, 12, 5, 1, 45, 5, 506000, tzinfo=tzlocal()) is not JSON serializable
Traceback (most recent call last):

【问题讨论】:

  • 如果您澄清哪一行代码导致了异常,这将有所帮助。我假设它是return response,因为reboot_db_instance 的返回值包含InstanceCreateTime,它是一个日期时间对象,并且不是JSON 可序列化的。如果您实际上不需要将整个响应返回给客户端,则返回更简单的内容。

标签: python amazon-web-services aws-lambda amazon-rds


【解决方案1】:

请使用以下内容,使用dumps返回json字符串:

import json
...
...
return json.dumps(response, default=str)

【讨论】:

  • @jarmod,非常感谢。它现在正在工作。我只是返回了一些更简单的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 2018-11-24
  • 2022-11-04
  • 1970-01-01
相关资源
最近更新 更多