[英]AWS Lambda Python with MySQL
I'm trying to connect to mysql from my AWS Lambda script. 我正在尝试从我的AWS Lambda脚本连接到mysql。
I did 我做到了
pip install --allow-external mysql-connector-python mysql-connector-python -t <dir>
to install mysql-connector-python in local directory. 在本地目录中安装mysql-connector-python。
I zipped the file and uploaded it to AWS Lambda where my python files are being executed. 我压缩了文件并将其上传到我正在执行python文件的AWS Lambda。
My scripts are executing correctly up to the point where I initialize a mysql connection. 我的脚本正确执行到我初始化mysql连接的程度。
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. 正在记录'即将为db设置连接',但是从未记录'设置DB连接'并且我的程序达到超时并停止执行。
What might I be doing wrong? 我可能做错了什么?
EDIT: This is my class that I'm calling from lambda_function.py 编辑:这是我从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. 当我在本地执行MySql.py时,我的代码工作正常。
My database gets updated but nothing happens when I run it from AWS. 我的数据库得到更新,但是当我从AWS运行它时没有任何反应。
Is it a MySQL instance on AWS (RDS) or on premise DB? 它是AWS(RDS)上的MySQL实例还是内部数据库? If RDS, check the NACL inbound rules of the vpc associated with your DB instance. 如果是RDS,请检查与数据库实例关联的vpc的NACL入站规则。 Inbound rules can allow/deny from specific IP sources 入站规则可以允许/拒绝来自特定IP源
when you did a zip file create. 当你创建一个zip文件时。 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. 这可能是您能够在本地执行但不能在lambda中执行的原因。
This is the syntax 这是语法
pip install module-name -t /path/to/PythonExampleDir pip install module-name -t / path / to / PythonExampleDir
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.