This example shows how to connect to an Oracle database (RDS or on-prem) from AWS Lambda using python. The official Oracle driver (cx_Oracle) requires the Oracle Instant Client with system dependencies to connect to an Oracle database, which does not fit well into a lambda package or layer. Therefore this example shows how to use Lambda container images to properly install the Oracle dependencies.
The following software is necessary on the client in order to build and deploy this example.
- Python 3.9.x (preferably with a dedicated virtualenv)
- Docker 19.x or above
- CDK version 2.3.0
In addition, this setup does not include an Oracle database, but assumes that one already exists. The deployment procedure outlined below will download and install the cx_Oracle driver and Oracle Instant Client, both are subject to separate licensing terms.
The following AWS services are used to run this example.
- AWS Lambda establish connection and run queries against Oracle database
- Elastic Container Registry (Amazon ECR) storage for lambda container image versions
- AWS Secrets Manager store credentials to connect to Oracle database
- AWS X-Ray providing tracing information for lambda
- AWS SQS dead letter queue for lambda failed lambda events
-
Install build and deployment dependencies:
python -m pip install -r requirements-dev.txt
-
Setup AWS profile to point to the account to deploy, either using AWS_PROFILE or through setting AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
-
Deploy stack using CDK
cd cdk cdk bootstrap cdk synth cdk deploy
-
Open SecretsManager in the AWS Console and adapt the connection settings in the secret
py-oracle-connection-credentials
-
Open the Lambda function
py-oracle-connection-example
in the AWS Console and specify the correct VPC settings in order to connect to the Oracle database. -
Execute the lambda function with an empty example payload (e.g.
{}
) -
Adapt the lambda handler as necessary to execute queries against Oracle database, see lambda_handler.py
-
Destroy all resources when tests are done
cd cdk cdk destroy
The example presented here opens an Oracle database connection per execution. This is not suitable for high-throughput scenarios. Therefore it is adviced to configure the reserved concurrency for the lambda according to the maximum open connections in parallel that the database allows for.
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.