1
votes

I have a flask restful API which works fine on EC2. Its able to connect to AWS SQL Server and can perform fetch/update. But when I upload to AWS lambda I get this error (I am using zappa to upload).

libodbc.so.2: cannot open shared object file: No such file or directory: ImportErrorTraceback (most recent call last): File "/var/task/handler.py", line 609, in lambda_handler return LambdaHandler.lambda_handler(event, context) File "/var/task/handler.py", line 240, in lambda_handler handler = cls() File "/var/task/handler.py", line 134, in init self.app_module = importlib.import_module(self.settings.APP_MODULE) File "/var/lang/lib/python3.6/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 955, in _find_and_load_unlocked File "", line 665, in _load_unlocked File "", line 678, in exec_module File "", line 219, in _call_with_frames_removed File "/var/task/app.py", line 8, in
import pyodbc ImportError: libodbc.so.2: cannot open shared object file: No such file or directory

I am using Python 3.6 and have the pyodbc installed in my virtual env and its working fine in EC2

My connection string is this:

app = Flask(__name__)

details = {
'server' : '*********ap-south-1.rds.amazonaws.com',
'database' : '*******',
'username' : '*****',
'password' : '*****'
}
params='DRIVER={{ODBC Driver 13 for SQL Server}};SERVER={server};PORT=1443; DATABASE={database};UID={username};PWD={password}'.format(**details)
params = urllib.parse.quote_plus(params)
app.config['SQLALCHEMY_DATABASE_URI'] = "mssql+pyodbc:///?odbc_connect=%s" % params

#######################
db = SQLAlchemy(app)

Is there any dependency like ODBC driver file that i need to place? Please Help

2

2 Answers

1
votes

You haven't included the library libodbc.so correctly in your Lambda deployment package. It can be fairly complicated to included system binaries like that in your Lambda deployment. Here's an example of what it takes to do that.

Luckily someone has published a Lambda Layer you can use instead.

0
votes

I was able to resolve this using Mark Suggestion. We need to add layers and other settings in zapaa setting file .

{
"dev": {
    "app_function": "app.app",
    "aws_region": "ap-south-1",
    "profile_name": "default",
    "project_name": "flask-app",
    "runtime": "python3.7",
    "s3_bucket": "<Name of bucket>",
    "manage_roles": false, 
    "vpc_config": { 
        "SubnetIds": [ <list of all subnet id's>],
        "SecurityGroupIds": [ "<>" ]
    },
    "layers":["<layers arn you want to add>"]
}

}