1

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

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks . Also i am a newbie , actually when Iwent to aws interface and added the layer by zip and tried there its working. But how can i add the same layer when i am using zappa. because whenever i do "Zappa update dev". It kind of remove layer also.
@user3809411 in your local Zappa settings, you would define a "layers" setting that includes the ARN of this layer.
Thanks Mark, I will give this a try
0

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>"]
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.