【发布时间】: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