-
-
Save lstroud/3229dbd880c27ee27a5b54ca45a26b54 to your computer and use it in GitHub Desktop.
Create Python Lambda package to connect to Oracle RDS
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment