Build Rest API using AWS Lambda function and On-Premise ORACLE Database

Muratakdeniz
4 min readFeb 28, 2020

I would like to share with you my experience with AWS Lambda and its relationship with Oracle Database. I hope that this post helps somebody who has similar issues.

It is not always possible to use AWS services. Some if not most of the time you have to deal with the existing new or legacy systems. So I will try to share the information that I have gathered during my search. Let starts, I am assuming that you have already knowledge about AWS and worked with AWS services.

VPC — Networking

Since you want to connect your on-premise database that means you have already your own VPC which has multiple subnets and connections to your on-premise datacenter via either Direct Connect, VPN or Transit Gateway.

For the configuration, I have used the Serverless framework. Therefore I don’t need to use the AWS console to configure, update or delete anything.

Following yml file example will explain everything. You can also get it from the link below.

Lambda Function

I have used NodeJs for the lambda function. Here you can see the yml definition

Here is the lamndaApi.js

If you have multiple functions and want to keep your code small to be able to edit in the browser then you should use Lambda Layers. As you can see I used three layers. You can create your own layers by yourself or you can download the one I used from the links below.

I created lambda layers separate from the project so even if I remove the project layers will stay there. That’s why I only referred to the arn number of layers. The first one is “oracledb” to be able to talk to the Oracle database. The second one is “knex” to be able to create queries easily. Last but not least “hapi-Joi” for request body validation. There is also a possibility that you can define your layers in yml file. For simplicity keep it separate. Please check out serverless.com for more information

Important! — Oracle Library

The main library for oracle is “node-oracledb”. But this library doesn’t work together with lambda. That’s why you should use “node-oracledb-for-lambda” or like me you can create your own layer using oracledb and oracle libraries. The reason why I used it as a layer is that because when you add this library with your function, the size of the package will increase and you can not edit your code on AWS console using the browser.

It is not a big issue but during development, it helps a lot. By the way size of the package does not affect the performance of the function. please check this article by Yan Cui.

Packaging the layer

The library files have to be zipped to upload AWS and the folder structure has to be exactly like this.

for more: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

Rest API

It is incredibly simple to expose the lambda function as a Rest API. All you need to do is add the following section under events.

The lambda will be exposed as a Get method Rest API. I used AWS Cognito for the authentication of API by JWT token, but there some other options as well. This may be another post in the future.

I hope you will find this post helpful. Please feel free to contact me if you have any questions.

Links

https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

http://knexjs.org

--

--