Test

Overview

Nowadays many enterprises move their decision services to clouds utilizing new serverless architecture. They simply upload their code and serverless takes care of everything required to run and scale this code with high availability. 

The most popular today’s serverless environment is AWS Lambda. It allows you to deploy and run your decision services without even thinking about servers and to pay only for the execution time your services consume. This reduced overhead lets developers reclaim time and energy that can be spent on developing great products which scale and that are reliable.

Why Amazon Web Services ?

AWS Lambda and AWS API Gateway have made creating serverless APIs extremely easy. You can simply upload your decision service to AWS Lambda, configure an API Gateway, and start responding toRESTful endpoint calls. However, the way you build and deploy your Lambda Decision Service is usually not as straightforward as it seems on the surface. This case study provides a sampling with all details of how to build AWS Lambda Decision Services without assuming any preliminary knowledge of the AWS environment.

Proposed Architecture

The developer can use Rule Designer as an Eclipse-based development environment of IBM Operational Decision Manager to create rule applications that automate the implementation of business policies. Once the rule application is created in the Rule Designer, the developer can migrate the application to a java standard edition environment to easily upload it to AWS Lambda which supports Java Runtimes(8,11). 

Once the rule application is deployed to AWS Lambda, you can invoke it as a lambda function over HTTPS by defining a custom REST API and endpoint using Amazon API Gateway, and then mapping individual methods, such as GET and POST, to the specific Lambda function.

Proposed Solution

What Should Be Pre-Installed

We assume that you’ve already installed:

  • Java 1.8 or later
What You Will Do

We will explain what you need to do to create, test, and deploy an ODM business decision model as AWS Lambda Function, and then execute it from a Java-based decision-making application.

Following step-by-step instructions below, you do the following:

  1. Create a  Hello World Rule Project from the Rule Designer.

  2. Migrate the project to a Java SE environment(Eclipse),  build and run it locally.

  3. Prepare the Java Project to be deployed to AWS Lambda

  4.  Upload it to AWS Lambda.

  5. Create a new AWS API that will provide you with an invocation URL.

  6. Use this URL to test the Lambda service from POSTMAN.  In the end, you will be ready to create, deploy, and execute your own AWS Lambda decision services.

Defining Decision Model

The standard ODM Rule Designer installation comes with a sample “Hello World” Classic Rule Project, that contains:

  • a single rule in a single package
  • a ruleflow
  • a BOM with a method

In a Nutshell, this decision model calculates the DateTime of the system and returns a greeting expression based on it “Good Morning! / Good Afternoon!”.

1. Create a Hello World Rule Project

Let’s assume that you opened your Rule Designer:

1.1 On the File menu, click New  > Rule Project.

1.2 Select the Hello World rule project template and then click Next.

1.3 In the Project name field, enter a name for your project.

1.4 If you want to save the project in your Eclipse workspace, keep the Use default checkbox selected.

1.5 click Finish.

The Rule Explorer view displays your new rule project.

1.6 Right-click on the project and select properties.

1.7 Change the Rule Engine property of the rule project to Decision engine instead of Classic rule engine and then click Apply and Close.

1.8 Back to the File menu, click New  > Project

1.9 Under the Project Designer folder select Java Project for Rules and then click Next

1.10  In the Project name field, enter a name for your project and click Next.

1.11  Select the previous classic rule project and click Next.

1.12  Select the Simple decision engine runner template and then click Finish.

The Rule Explorer view displays your new rule project as Java Project.

2. Migrate the Project to a Java SE environment (Eclipse):

Let’s assume that you opened your Eclipse IDE:

2.1 On the File menu, click Open Projects from File System

2.2 Click on Directory and browse to your Hello World Java Project

 

2.3 Select the project and then click on Finish.

The Rule Explorer view displays the Java Project full of errors!

Well, first of all, you need to add the jrules-engine.jar library.

2.4 Right-click on the project, select Build Path > Add External Archives, and then browse to your

jrules-engine.jar and select it.

 And that’s the tea!

2.5 Right-click on the project, select Run As > Java Application > SimpleDecisionEngineRunner

The Project is Built and Run successfully!

3. Prepare the Java Project to be deployed to AWS Lambda:

First of all, you should have the following folder structure:

project-dir/pom.xml

project-dir/src/main/java/  (your code goes here)  

Your project structure should be something like this:

3.1 Right-click on the project and select Configure > Convert to Maven Project

3.2 Fill in the Group Id & Artifact Id fields accordingly to your package(com.sb.greetings) and click on   Finish

The POM is now generated!

3.3 To enable AWS lambda, add the following dependency to the project:

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>

3.4 We also need the Maven Shade Plugin to build the lambda application:

<plugin>
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>

3.5 Install the  jrules-engine.jar into your local maven repository by running the following command:

mvn install:install-file -Dfile= -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar

Where:

  • <path-to-file>: Path to the JAR to install
  • <group-id>: Group id of the JAR to install
  • <artifact-id>: Artifact id of the JAR to install
  • <version>:  Version of the JAR

It should look something like this:

mvn install:install-file -Dfile=C:\Users\ASUS\Desktop\mydeployment\jrules-engine.jar -DgroupId=com.odm
-DartifactId=ruleEngine -Dversion=1.0 -Dpackaging=jar

3.6 Add directly the jrules-engine.jar dependency as system scope:

<dependency>
<groupId>com.odm</groupId>
<artifactId>ruleEngine</artifactId>
<version>1.0</version>
</dependency>

The POM file is now ready!

To invoke a lambda function, we still need to specify a handler

3.7 Create a custom MethodeHandle: 

  • Navigate to the main function in the src folder and replace the code with the following:
public String handleRequest(Context context) {

try {
File file = new File("ruleArchive1.dsar");
EngineLoader loader = new EngineLoader(file);
EngineDefinition definition = loader.load();
Engine engine = definition.createEngine();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
// IMPORTANT: Save the old System.out!
PrintStream old = System.out;
// Tell Java to use your special stream
System.setOut(ps);

EngineOutput output = engine.execute(engine.createInput());

System.out.flush();
System.setOut(old);
// Show what happened
return baos.toString();


} catch (Exception exception) {
exception.printStackTrace(System.err);
}
return "fail";
}

3.8 Copy the ruleArchive1.dsar file located at the root of the directory to the src/main/java folder

3.9 add to your POM under the tag:

<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.dsar</include>
</includes>
</resource>
</resources>

3.10 Build the Deployment file:

  • With everything configured, we can create the deployment file by simply running:
mvn clean package shade:shade

The project is finally packaged and ready to be deployed!

Uploading Decision Model to AWS Lambda

Let’s assume that you signed in to the AWS Lambda console:

1.Click on Create function. The next screen will look as follows:

2.Enter Function name Greeting

3. Select Java 8 from the combo-box Runtime

4. Click on a little triangle on the left of Choose or create an execution role and select Create a new role with basic Lambda permissions

5. Click on Create function. It will display the following screen:

6. Click on the button Upload and browse to the already generated shaded jar from the folder target.

7. In the box Handler replace example.Hello::handleRequest with com.sb.greetings.SimpleDecisionEngineRunner::handleRequest. Make sure that you use two colons.

8. Click on the button Save in the top right corner. 

Our AWS Lambda Function is ready. Now we need to create an API for its invocation.

Creating API For Lambda Invocation

Let’s switch to AWS API Gateway. 

  1. Click on the menu item Services and find API Gateway.

  2. On the next screen click on Create API and it will display:

Let’s switch to AWS API Gateway. 

3. Click on the menu item Services and find API Gateway.

4. On the next screen click on Create API and it will display:

It will display the following :

5. Click on the input box below Lambda Function and choose Greeting. Click on the button Save. It will display a box similar to this one:

Click OK. It will display this view:

6. Get back to the combo-box Actions and select Deploy API. It will display this dialog:

7. Select [New Stage] as a Deployment stage and enter a description similar to the above ones. Click on the button Deploy. You will see the following screen:

You may modify different deployment options later on, but by now we already have our Invoke URL specified in the blue box on the top. Right-click on this URL and select Copy link address. Click on the button Save Changes. Our Lambda function is successfully deployed and we know its invocation URL. We may test this lambda function.

Testing Lambda Decision Service

You may test the  Lambda Decision service using different ways, but here we will describe 2 testing facilities:

  • Using Lambda Test
  • Using POSTMAN

1. Testing using Lambda:

First, we will test the AWS Lambda function using Lambda itself. 

1.1 Select Lambda function Greeting and from the screen,  click on the combo-box Select a test event choose Configure test events. Configure the dialog as follow:

1.2 Click on the button Create. After the dialog is closed, click on the button Test. It will display

2. Testing using Postman:

POSTMAN is a popular tool that can be downloaded for free from https://www.getpostman.com/. 

After installation and start, you may fill out this POSTMAN’s form:

  1. In this form, select the method GET (from the drop-down list), and paste the copied Invoke URL to the box next to the GET

  2. After a click on Send, the POSTMAN will send the proper HTTP request to the Lambda function, which will execute the decision service and will return back the greeting expression!

A cloud in mind ?
Contact us !

How can we help ?