Sample AWS Lambda Function to Monitor Oracle Database

I wrote a very simple AWS Lambda function to demonstrate how to connect an Oracle database, gather the tablespace usage information, and send these metrics to CloudWatch. First, I wrote this lambda function in Python and then I had to re-write it in Java. As you may know, you need to use cx_oracle module to connect Oracle Databases with Python. This extension module requires some libraries which are shipped by Oracle Database Client (oh God!). It’s a little bit tricky to pack it for the AWS Lambda. Good thing is, I found a great document which explains all necessary steps.

Here’s the main class which will be used by Lambda function:

Here’s the step by step explanation:

Line 15: I created a class named Monitoring – this will be the handler class. To be able to test it, it accepts JSON objects, this is why it implements “RequestHandler, String>”.
Line 16: Definition of the function required to handle requests
Line 18: Taken from the AWS documents. It’s the object which we use to push metrics to CloudWatch
Line 20-24: Loading the JDBC class to be able to connect an Oracle database
Line 26-29: Defining the connection and credentials. As you see I fetch the required parameters from environment because Lambda let you define env variables
Line 32-33: Connecting to the database
Line 35: Creating a statement object to run queries
Line 37: As a sample, I query dba_tablespace_usage_metrics to get the usage percentage for tablespaces
Line 38: Loop for each record
Line 40-47: Preparing the metric I want to push to CloudWatch – The metric will stored as “Space Used (pct) for each tablespace in the “Databases” namespace in the customer metrics
Line 49: Pushes the metric data
Line 53-55: Mandatory exception handling block
Line 57: Returns a result (success)

You many notice a lot of “System.getenv” calls. I used it to read the environment parameters from the Lambda function.

You can download the Maven project from my github repository. After you build the maven project, you need to upload the JAR file to your S3 bucket:

I used this JAR file to create a lambda function on AWS Lambda console. My package name is “com.gokhanatil.samplelambda”, my class name is “Monitoring”, and the function name is “handleRequest”, this is why I entered “com.gokhanatil.samplelambda.Monitoring::handleRequest” as handler. Please note that, you need to give required permissions to grant access to CloudWatch (ie CloudWatchFullAccess), and AWSLambdaVPCAccessExecutionRole. You also need to enter the VPC information (Subnets: pick two subnets that can reach your Oracle instance, Security groups: select one that gives you access to your Oracle instance).

To schedule this job to run periodically, you can use “CloudWatch events”. This event will be triggered every 15 minutes, and launch the lambda function.

After enabling the lambda function, new metrics starts to appear in the CloudWatch dashboard. It’s possible to create an alarm for these metrics using the console or AWS CLI commands.

I hope it helps.

Please share
  •  
  •  
  •  
  •  
  •  
  •  

AWS Big Data Specialist. Oracle Certified Professional (OCP) for EBS R12, Oracle 10g and 11g. Co-author of "Expert Oracle Enterprise Manager 12c" book published by Apress. Awarded as Oracle ACE (in 2011) and Oracle ACE Director (in 2016) for the continuous contributions to the Oracle users community. Founding member, and vice president of Turkish Oracle User Group (TROUG). Presented at various international conferences including Oracle Open World.

1 Comment

  1. Yasir

    Amazing talent you are Gokhan!

    Just out of curiosity,did you know Python and Java before also or you learned them after joining AWS as part of your job role?
    The reason I am asking is Oracle guys usually are away from development stuff.

Leave Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.