-
-
Save muhammedbasilsk/99ab5052d49cef0b9633c1f86948c8e2 to your computer and use it in GitHub Desktop.
Setup virtualenv to access Oracle RDS from AWS Lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment