Search code examples
amazon-web-servicessqlalchemyaws-lambdapython-3.6amazon-linux

create_engine in sqlalchemy not working in python 3.6 runtime for aws lambda


I successfully tested pandas, numpy, and sqlalchemy in an Amazon Linux docker image using python 3.6. I was able to import, use, and connect to a database in the virtual environment using create_engine from the sqlalchemy module in python 3.6.

I then exported all the dependencies and built a python deployment package to run it in AWS Lambda but for some reason I keep getting an error for create_engine in lambda.

module 'sqlalchemy' has no attribute 'create_engine': AttributeError

This is my code:

import pandas as pd
import numpy as np
import sqlalchemy
from datetime import datetime, timedelta

def lambda_handler(event, context):

    engine = sqlalchemy.create_engine("DB_URI")

    return "Hello world!"

However, if I simply comment out the line where I call create_engine, I get my "Hello world!" response.

I don't get why create_engine is not working in this environment when it worked perfectly fine in the identical docker environment. Any ideas?


Solution

  • I figured it out. I had a rookie mistake when I was zipping up my file and didn't use the -r option which meant only the top level of my python module folders where getting zipped up. This explains why I wasn't getting an import error but none of the actual methods were working.

    So to reiterate, the solution was adding the -r option to my zip operation to add all the files recursively:

    zip -r package.zip *