Han Wang
Han Wang

Reputation: 89

Unable to import module 'lambda_function': No module named 'mysql'

I'm trying to send data to a MySQL database hosted on AWS' RDS using a Lambda function. However, when I try to import the mysql module with import mysql.connector I get a "Runtime.ImportModuleError" error.

I've tried finding a python file for the mysql.connector online to upload to the function with no luck.

import mysql.connector

def lambda_handler(event, context):
    mydb = mysql.connector.connect(
            host="test.url.us-west-2.rds.amazonaws.com",
            user="root",
            passwd="password",
            database="test")

     mycursor = mydb.cursor()
     sql = "INSERT INTO table VALUES ('test')"
     mycursor.execute(sql)

     mydb.commit()
     return {
        status: 'success'
     }

I would like to successfully execute the SQL command above to the remote RDS database. But I keep getting "Unable to import module 'lambda_function': No module named 'mysql'"

Upvotes: 3

Views: 6078

Answers (1)

kungphu
kungphu

Reputation: 4859

When deploying a Python function to AWS Lambda, you need to bundle your dependencies with your Python file(s).

If your function depends on libraries other than the SDK for Python (Boto 3), install them to a local directory with pip, and include them in your deployment package.

Upvotes: 1

Related Questions