简体   繁体   中英

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

I'm trying to create an AWS Lambda function in Python to connect to an Oracle Database (for now, just a test connection). But I'm not got success to complete the flow. Every time I see this error message:

{
      "errorMessage": "Unable to import module 'lambda_function': No module named 'cx_Oracle'",
      "errorType": "Runtime.ImportModuleError"
    }

I've created a virtualenv at Ubuntu WSL, install the Oracle InstantClient on the lib folder, install cx_oracle by pip at sites-package folder, and create my lambda function at the same folder, zip everything, upload at my S3 and put to run.

Can anyone help me?

My code:

    import cx_Oracle

    # Yeah, you need this
    with open('/tmp/HOSTALIASES', 'w') as f: f.write(f'{os.uname()[1]} localhost\n')

    # Oracle away!
    def lambda_handler(event, context):
        return str(
            cx_Oracle.connect(
                'username',
                'password',
                cx_Oracle.makedsn(
                    'rds.amazonaws.com', 1521, 'SOME_SID',
                )
            ).cursor().execute('SELECT 42 FROM DUAL').fetchone()
        )

My sites-package folder:文件夹

** My lambda config: ** 拉姆达

AWS Lambda runs with Amazon Linux, so you need the compatible library of cx_Oracle, in this case manylinux.whl

just download the whl, unzip it, and copy the .so file into your lib folder.

That way Lambda will recognize cx_Oracle.

The folder structure should look like this: structure

for python3.7 additional path in lambda use PYTHONPATH environment variable, and save your libs in this folder.

在此处输入图片说明

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM