1

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 Answers 2

4

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

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

Comments

0

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

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.