4

I'm trying to connect to an Oracle DB using AWS Lambda Python code.

My code is below:

import sys, os
import cx_Oracle
import traceback

def main_handler(event, context):

  # Enter your database connection details here
  host = "server_ip_or_name"
  port = 1521
  sid = "server_sid"
  username = "myusername"
  password = "mypassword"

  try:
    dsn = cx_Oracle.makedsn(host, port, sid)
    print dsn
    connection = cx_Oracle.Connection("%s/%s@%s" % (username, password, dsn))
    cursor = connection.cursor()
    cursor.execute("select 1 / 0 from dual")
  except cx_Oracle.DatabaseError, exc:
    error, = exc.args
    print >> sys.stderr, "Oracle-Error-Code:", error.code
    print >> sys.stderr, "Oracle-Error-Message:", error.message
    tb = traceback.format_exc()
  else:
    tb = "No error"
  finally:
    print tb


if __name__ == "__main__":
  main_handler(sys.argv[0], None)

If have already added all dependencies in "lib" folder, thanks to AWS Python Lambda with Oracle

When running this code, I'm getting: DatabaseError: ORA-21561: OID generation failed

i've tried to connect using IP of the Oracle server and the name: same error.

Here is the output of the error

Oracle-Error-Code: 21561
Oracle-Error-Message: ORA-21561: OID generation failed

Traceback (most recent call last):
  File "/var/task/main.py", line 20, in main_handler
  connection = cx_Oracle.Connection("%s/%s@%s" % (username, password, dsn))
DatabaseError: ORA-21561: OID generation failed

For those who have successfully run the CX_Oracle in AWS Lambda Python, can you please help ?

Thanks

1
  • FYI, you can do cx_Oracle.Connection(username, password, dsn) instead of creating a connect string -- which cx_Oracle will then have to pull apart into the separate components anyway! Commented Aug 31, 2016 at 16:45

3 Answers 3

7

Ok, here is the explanation: Oracle has a funny behavior where if the hostname given by hostname can't be resolved, it will fail connecting to the DB. Fortunately, in Linux, one can override the DNS entry for a session by writing an alias file in /tmp, then setting the environment variable HOSTALIASES to that file.

So adding this code to my function help to generate this file, and now I can successfully connect:

f = open('/tmp/HOSTALIASES','w')
str_host = os.uname()[1]
f.write(str_host + ' localhost\n')
f.close()

Hope it can help someone else !

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

2 Comments

Thanks... Helped me today in getting the Oracle node library to work with Lambda. Apart from ORA-2156, setting the alias also resolves the other related oracle errors: ORA-24408 could not generate unique server group name, ORA-24454: client host name is not set
Thank you! Such a pain to get this working. This solved my last of many problems.
0

See the following other question for the resolution to this problem.

sqlplus remote connection giving ORA-21561

Effectively the client requires a host name in order to generate a unique identifier which is used when connecting to the database.

10 Comments

/etc/hosts cannot be modified in the AWS Lambda so this solution cannot be applied.
That's the only solution I've ever seen for that problem. You can try using the name instead of the IP address. From what I understand the IP address has to be able to be resolved to a name in some fashion. And AWS has to give you some ability to do that!
I edited the answer to include the information about the requirement for a valid host name on the client. Hopefully there is some way for AWS Lambda to configure that information for you!
already tried with name or IP of my Oracle server, and I did get the same behavior. I will edit the question to add this one
The name and IP of the Oracle server are not relevant for this error. The client is what needs a name in order for the OID to be generated.
|
0

The accepted solution for this correct, but please also be aware that the HOSTALIASES mechanism does require working DNS (as strange as it sounds).

I struggled with this for a few hours having implemented the accepted solution and realised that I was not permitting outbound DNS on the security group attached to by Lambda function VPC interface (my connection was by IP address to Oracle DB, so initially did not think this was required).

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.