AWS Python Lambda with Oracle - OID Generation Fai

2019-08-20 05:42发布

问题:

I am trying to connect to oracle on rds using lambda with python using cx_oracle package but i get:

ORA-21561: OID generation failed: DatabaseError.

Even after adding file /tmp/HOSTALIASES with the lambda-server-name localhost. Also added HOSTALIASES to lambda environment variables. Referd from: AWS Python Lambda with Oracle - OID Generation Failed.

How to resolve this OID generation problem in aws lambda

Here is my code

import cx_Oracle
import os
import sys
import time

# sys.path.append('lib')
# os.environ['ORACLE_SID'] = 'DEVDB'


with open('/tmp/HOSTALIASES', 'w') as hosts_file:
    hosts_file.write('{} localhost\n'.format(os.uname()[1]))

def orcl_fetch_records(event, context):
    # print (sys.path)
    # print (os.listdir(os.getcwd()))
    # print (os.environ['LD_LIBRARY_PATH'])

    # print (os.environ['ORACLE_HOME'])
    # print (sys.path)
    print (os.environ['HOSTALIASES'])
    with open('/tmp/HOSTALIASES', 'r') as hosts_file:
        print hosts_file.read()
    dsn = cx_Oracle.makedsn("aws-rds-oracle-server-name", "1521", "SID")
    print (dsn)
    conn = cx_Oracle.connect("username", "password", dsn)
    print ("Oracle DB version = " + conn.version)
    cur = conn.cursor()
    cur.execute('select * from lambda_test')
    for result in cur:
        print (result)
    cur.close()
    conn.close()

Output:

ORA-21561: OID generation failed: DatabaseError Traceback (most recent call last): File "/var/task/orcl_fetch_function.py", line 25, in orcl_fetch_records conn = cx_Oracle.connect("username", "password", dsn) DatabaseError: ORA-21561: OID generation failed

回答1:

That error is generally due to the fact that your host name cannot be determined. This post may be of help: https://osric.com/chris/accidental-developer/2015/10/connecting-to-oracle-instance-in-aws-rds/



回答2:

I ran into this same issue and found that the HOSTALIASES mechanism requires working DNS. If your Lambda function is VPC attached, you must allow outbound DNS to either Amazon VPC DNS or your own internal DNS server if you have one.