我正在尝试编写一个简单的Lambda函数来连接和执行Aurora MySQL数据库操作。
函数的名称为LogStream
,Python文件为lambda_function.py
,其中有我的handler function
。
我已经从CLI更新了处理程序,并成功完成了
{
"FunctionName": "LogStream",
"FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxx:function:LogStream",
"Runtime": "python3.6",
"Role": "arn:aws:iam::xxxxxxx:role/LambdaRole",
"Handler": "lambda_function.handler",
"CodeSize": 777,
"Description": "",
"Timeout": 3,
"MemorySize": 128,
"LastModified": "2020-10-14T16:26:20.733+0000",
"CodeSha256": "gEM+bUDuBHBGiwPecUv+IT0QEBzR28Ave94F8hc=",
"Version": "$LATEST",
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "7403730c-5349-4766-a11f-c68d66420a4a",
"Layers": [
{
"Arn": "arn:aws:lambda:us-east-1:xxxxxx:layer:pymysql:1",
"CodeSize": 1621906
}
],
"State": "Active",
"LastUpdateStatus": "Successful"
}
这是完整的代码:
import sys
import logging
import rds_config
import pymysql
#rds settings
rds_host = "aurora-mysql-instance-1.xxxxxx.us-east-1.rds.amazonaws.com"
name = "admin"
password = "xxxxxx"
db_name = "Test"
logger = logging.getLogger()
logger.setLevel(logging.INFO)
try:
conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5)
except pymysql.MySQLError as e:
logger.error("ERROR: Unexpected error: Could not connect to MySQL instance.")
logger.error(e)
sys.exit()
logger.info("SUCCESS: Connection to RDS MySQL instance succeeded")
def handler(event, context):
"""
This function fetches content from MySQL RDS instance
"""
item_count = 0
with conn.cursor() as cur:
cur.execute("create table Employee ( EmpID int NOT NULL, Name varchar(255) NOT NULL, PRIMARY KEY (EmpID))")
cur.execute('insert into Employee (EmpID, Name) values(1, "Joe")')
cur.execute('insert into Employee (EmpID, Name) values(2, "Bob")')
cur.execute('insert into Employee (EmpID, Name) values(3, "Mary")')
conn.commit()
cur.execute("select * from Employee")
for row in cur:
item_count += 1
logger.info(row)
#print(row)
conn.commit()
return "Added %d items from RDS MySQL table" %(item_count)
我添加了一个Layer
依赖项的pymysql
。
我无法弄清为什么调用失败并出现错误:
{"errorMessage": "Unable to import module 'lambda_function'"}