Skip to content

Instantly share code, notes, and snippets.

@niranjv
Created June 17, 2017 17:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save niranjv/9b9d162a430a9e6f0701d40847a00fca to your computer and use it in GitHub Desktop.
Save niranjv/9b9d162a430a9e6f0701d40847a00fca to your computer and use it in GitHub Desktop.
Create Python Lambda package to connect to Oracle RDS
# Create and upload a deployment package for a Python Lambda function
# Assumes EC2 instance is associated with an IAM role with permissions to access S3 & Lambda
# Ref: http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html
# This script is meant to be used after virtualenv has been setup to connect to Oracle RDS
# see https://gist.github.com/niranjv/2a576a13afe1b323e9e9600ed7de03ef
# Steps:
# - create temp dir
# - copy all Python scripts and lib files to temp dir
# - zip contents of temp dir
# - upload zip file to S3
# - update Lambda function code with contents of zip file
BASE_DIR=/home/ec2-user/oracle_test # virtualenv created previously
PACKAGE_DIR=$(mktemp -d -p `pwd`)
ZIP_FILE=package.zip
S3_BUCKET=<S3 bucket>
REGION=<AWS region>
LAMBDA_FUNCTION_NAME=<name of existing Lambda function>
cp $BASE_DIR/<PYTHON_SCRIPTS> $PACKAGE_DIR
find $BASE_DIR/lib/python2.7/site-packages/ -exec cp {} $PACKAGE_DIR \; # assuming virtualenv runs Python 2.7
mkdir $PACKAGE_DIR/lib
find $BASE_DIR/../instantclient/ -exec cp {} $PACKAGE_DIR/lib \; # assumes instantclient libs are installed in "/home/ec2-user/instantclient"
cp /lib64/libaio.so.1.0.0 $PACKAGE_DIR/lib/libaio.so.1 # run "yum -y install libaio-devel" if file is missing
cd $PACKAGE_DIR # must cd into dir before zipping
zip -qr $ZIP_FILE .
mv $ZIP_FILE $BASE_DIR
aws s3 cp $BASE_DIR/$ZIP_FILE s3://$S3_BUCKET/
# Lambda function must already exist; cannot create a new Lambda function with update-function-code
aws lambda update-function-code --region $REGION --function-name $LAMBDA_FUNCTION_NAME --s3-bucket $S3_BUCKET --s3-key $ZIP_FILE
@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

@johngriffin00
Copy link

Hi, this was really helpful and looked like it all worked for me but when I try and invoke the function on Lambda i get a really unhelpful result / error when invoked from the AWS CLI on my EC2 Linux machine:

{"errorMessage": "Unable to import module 'lambda_function'"}

Same when I execute from within the AWS console. When I copied and created this sh file locally and executed, I did get a bunch of 'ommitting directory' messsages like the following:

cp: omitting directory ‘/home/ec2-user/oracle_test/lib/python2.7/site-packages/pip/_internal/operations’

One of the things I struggled with is what should be in <PYTHON_SCRIPTS> as the function I am updated already MUST be on AWS Lambda, right?

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