我正在尝试设置将启动 RDS 重启的 AWS lambda。这是我的 lambda 函数:
I am trying to setup an AWS lambda that will start an RDS Reboot. Here is my lambda function:
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 json 字符串使用 dumps:
import json
...
...
return json.dumps(response, default=str)
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 json 字符串使用 dumps:
import json
...
...
return json.dumps(response, default=str)