0

I'm using python to try and connect to a DB. This code worked and something in my environment changed so that the host in not present/accessible. This is as expected. The thing that I'm trying to work out is, I can't seem to catch the error of this happening. This is my code:

def create_db_connection(self):
    try:
        message('try...')

        DB_HOST = os.environ['DB_HOST']
        DB_USERNAME = os.environ['DB_USERNAME']
        DB_PASSWORD = os.environ['DB_PASSWORD']

        message('connecting...')

        db = mysql.connector.connect(
            host=DB_HOST,
            user=DB_USERNAME,
            password=DB_PASSWORD,
            auth_plugin='mysql_native_password'
        )

        message('connected...')

        return db
    except mysql.connector.Error as err:
        log.info('bad stuff happened...')
        log.info("Something went wrong: {}".format(err))
        message('exception connecting...')
    except Exception as ex:
        log.info('something bad happened')
        message("Exception: {}".format(ex))

    message('returning false connection...')
    return False

I see up to the message('connecting...') call, but nothing afterwards. Also, I don't see any of the except messages/logs at all.

Is there something else I need to catch/check in order to know that a DB connection attempt has failed?

This is running inside an AWS Lambda and was working until I changed some subnets/etc. The key thing is I want to catch it no longer being able to connect.

0

The issue is most likely that your lambda function is timing out before the database connection is timing out.

First, modify the lambda function to execute for 60 seconds and test. You should find after about 30 seconds you will see the connection to the database timeout.

To resolve this issue, modify the security group on the database instance to include the security group configured for lambda. Use this entry to open a the correct port 3306

New contributor
Joseph is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.