3

I'm trying to connect to mysql from my AWS Lambda script.
I did
pip install --allow-external mysql-connector-python mysql-connector-python -t <dir>
to install mysql-connector-python in local directory.
I zipped the file and uploaded it to AWS Lambda where my python files are being executed.
My scripts are executing correctly up to the point where I initialize a mysql connection.
I have this

log('about to set connection for db')
connection = mysql.connector.connect(user=DB_USER, password=DB_PASSWORD, host=DB_HOST, database=DB_DATABASE)
query = "SELECT * FROM customers WHERE customer_email LIKE '%s' LIMIT 1"
log('set connection for DB')

'about to set connection for db' is being logged but 'set connection for DB' is never logged and my program hits a timeout and stops executing.

What might I be doing wrong?

EDIT: This is my class that I'm calling from lambda_function.py

import mysql.connector
import logging
from mysql.connector import errorcode

class MySql( object ):

    USER    =None
    PASSWORD=None
    HOST    =None
    DATABASE=None


    logger = logging.getLogger()
    logger.setLevel( logging.INFO )

    def __init__(self, user, password, host, database):
        global USER, PASSWORD, HOST, DATABASE
        USER        = user
        PASSWORD    = password
        HOST        = host
        DATABASE    = database

    def getId( self, customer_email ):
        email_exists = False

        connection = mysql.connector.connect(user=USER, password=PASSWORD, host=HOST, database=DATABASE)
        query = "SELECT * FROM customers WHERE customer_email LIKE '%s' LIMIT 1"

        cursor = connection.cursor()
        cursor.execute( query % customer_email )
        data = cursor.fetchall()
        id = None
        for row in data :
            id = row[1]
            break

        cursor.close()
        connection.close()
        return id

    def insertCustomer( self, customer_email, id ):
        log('about to set connection for db')
        connection = mysql.connector.connect(user=USER, password=PASSWORD, host=HOST, database=DATABASE)
        log('set connection for DB')
        cursor = connection.cursor()
        try:
            cursor.execute("INSERT INTO customers VALUES (%s,%s)",( customer_email, id ))
            connection.commit()
        except:
            connection.rollback()
            connection.close()
    def log( logStr):
        logger.info( logStr )

def main():
    user = 'xxx'
    password = 'xxx'
    host = ' xxx'
    database = 'xxx'

    mysql = MySql( user, password, host, database )

    id = mysql.getId('testing')
    if id == None:
        mysql.insertCustomer('blah','blahblah')
    print id

if __name__ == "__main__":
    main()

When I execute the MySql.py locally my code works fine.
My database gets updated but nothing happens when I run it from AWS.

3
  • Perhaps your host value is wrong, which could result in a network timeout. Commented Nov 13, 2015 at 5:45
  • Also, what does the log function do, can you show the code for it? It might be the problem. Commented Nov 13, 2015 at 5:46
  • Check the security groups Commented Jun 9, 2016 at 14:48

2 Answers 2

2

Is it a MySQL instance on AWS (RDS) or on premise DB? If RDS, check the NACL inbound rules of the vpc associated with your DB instance. Inbound rules can allow/deny from specific IP sources

Sign up to request clarification or add additional context in comments.

1 Comment

that's what I figured out. It is aws rds. I had to switch over to DynamoDB.
0

when you did a zip file create. Did you do a pip to target directory. I have enclosed the syntax below.This copies the files to your target directory to zip.

That may be the reason you are able to execute it locally but not in a lambda.

This is the syntax

pip install module-name -t /path/to/PythonExampleDir

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.