I am trying to setup an AWS lambda that will start an RDS Reboot. Here is my lambda function:

Viewed 57
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"
}

However when I run I get the following 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):
1 Answers

Please use the following, to return json string using dumps:

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