1
votes

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: folder

** My lambda config: ** lambda

2

2 Answers

4
votes

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

0
votes

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

enter image description here

enter image description here