I've created a new lambda function in Python with a handler called handler
. In the Configuration section, AWS requires me to put the name of the function in the form file-name.function-name
(as described here).
However I have no idea what the filename is supposed to be. I've created a blank function and didn't specify a filename at any point. My function name is "MySQLTest" so I've tried various things like "MySQLTest.handler", "my-sql-test.handler", "mysqltest.handler" but none of it seems to work.
Any idea what I should put as a filename?
For info, this is the test code I'm using:
import sys
import logging
import rds_config
import pymysql
#rds settings
rds_host = "*******"
# "rds-instance-endpoint"
name = rds_config.db_username
password = rds_config.db_password
db_name = rds_config.db_name
port = 3306
logger = logging.getLogger()
logger.setLevel(logging.INFO)
server_address = (rds_host, port)
try:
conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5)
except:
logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
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
try:
with conn.cursor() as cur:
cur.execute("create table Employee3 ( EmpID int NOT NULL, Name varchar(255) NOT NULL, PRIMARY KEY (EmpID))")
cur.execute('insert into Employee3 (EmpID, Name) values(1, "Joe")')
cur.execute('insert into Employee3 (EmpID, Name) values(2, "Bob")')
cur.execute('insert into Employee3 (EmpID, Name) values(3, "Mary")')
cur.execute("select * from Employee3")
for row in cur:
item_count += 1
logger.info(row)
#print(row)
finally:
conn.close()
return "Added %d items from RDS MySQL table" %(item_count)
If you are adding the function via the AWS Console the name you supply should be <name of lambda function>.handler
(assuming you called your Python function handler
.
So, if the name of my Lambda function is fooBar
and my Python function is called handler
I would use fooBar.handler
.