Skip to content

Instantly share code, notes, and snippets.

@niranjv
Last active December 19, 2019 19:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save niranjv/2a576a13afe1b323e9e9600ed7de03ef to your computer and use it in GitHub Desktop.
Save niranjv/2a576a13afe1b323e9e9600ed7de03ef to your computer and use it in GitHub Desktop.
Setup virtualenv to access Oracle RDS from AWS Lambda
# This script allows Python functions in AWS Lambda to query an Oracle RDS instance
# It is meant to run on an EC2 instance running Amazon Linux
# The script has been tested successfully on a t2.micro EC2 instance (Root device type: ebs; Virtualization type: hvm)
# running Amazon Linux AMI 2017.03.0 (HVM), SSD Volume Type - ami-c58c1dd3
# and was developed with the help of AWS Support
# Steps:
# - install Oracle Instant Client
# - create virtualenv
# - install cx_Oracle within virtualenv
# Install Oracle Instant Client
# Download packages for appropriate version of Oracle from http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
# Below, packages for Oracle 11.2g are downloaded to the home directory (/home/ec2-user)
unzip instantclient-sdk-linux-11.2.0.3.0.zip && rm instantclient-sdk-linux-11.2.0.3.0.zip
unzip instantclient-basic-linux.x64-11.2.0.3.0.zip && rm instantclient-basic-linux.x64-11.2.0.3.0.zip
unzip instantclient-sqlplus-linux.x64-11.2.0.3.0.zip && rm instantclient-sqlplus-linux.x64-11.2.0.3.0.zip
ln -s instantclient_11_2/ instantclient
ln -s $(basename `ls instantclient/libclntsh.so.*`) instantclient/libclntsh.so
export ORACLE_HOME=/home/ec2-user/instantclient
export LD_LIBRARY_PATH=/home/ec2-user/instantclient:$LD_LIBRARY_PATH
# Create virtualenv with Python 2.7 & install cx_Oracle Python module
virtualenv oracle_test
source oracle_test/bin/activate
pip install cx_Oracle
# Output should be similar to:
# Collecting cx_Oracle
# Using cached cx_Oracle-5.3.tar.gz
# Building wheels for collected packages: cx-Oracle
# Running setup.py bdist_wheel for cx-Oracle ... done
# Stored in directory: /home/ec2-user/.cache/pip/wheels/1d/27/9f/2e70740c08ff571d840d96e7782d16553468c46a2168604a5f
# Successfully built cx-Oracle
# Installing collected packages: cx-Oracle
# Successfully installed cx-Oracle-5.3
# ----------
# To connect to Oracle in Python script:
#
# import cx_Oracle
# dsn = cx_Oracle.makedsn(<RDS_HOSTNAME>, <PORT>, <SID>)
# conn = cx_Oracle.connect(<USERNAME>, <PASSWORD>, dsn)
# print "Oracle DB version = " + conn.version
# conn.close()
# Ref:
# http://www.oracle.com/technetwork/articles/dsl/python-091105.html
# http://cx-oracle.readthedocs.io/en/latest/cursor.html
@MatB30
Copy link

MatB30 commented Sep 22, 2017

Hi, how did you resolve the "OID generation failed" error?

@MatB30
Copy link

MatB30 commented Nov 24, 2017

Answer: DNS support needs to be enabled on the VPC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment