Building Python Rules Engine: Lambda and S3

Learn how to deploy and utilize a serverless rules engine.

Apr 4, 20236 min read
Python Business Rules Engine

In today's world, businesses need to be agile, flexible, and efficient to stay competitive. To achieve this, they need to implement technologies that allow them to process and analyze data quickly and accurately. One technology that has gained popularity in recent years is the serverless computing model.

Serverless computing allows developers to write and run code without having to worry about managing servers or infrastructure. This model is cost-effective, and scalable, and reduces the time it takes to deploy code to production.

In this article we will learn how to:

We will start by assuming you already have AWS Account.

Create an Amazon S3 bucket

To create a new bucket login into your AWS Console and go to the link: https://s3.console.aws.amazon.com/s3/buckets

Next, press Create on the right side.

Amazon S3 Buckets

In the forms enter a unique bucket name and select your region.

Create Amazon S3 Bucket

Scroll to the bottom, leave other fields as default and press Create Bucket.

After finishing you should see your new bucket in the list.

Amazon S3 Buckets - created

We have set up AWS S3 Bucket!

Create Rules Engine Lambda Function

Now, we will create a new Lambda Function and will attach Zen Engine Layer.

To use a Zen Engine in a Lambda environment we have two options:

  1. Bundle Zen Engine dependency with the Python program,
  2. Add Zen Engine Layer that has a built-in dependency.

The lambda layer is a smart way to package the dependencies and libraries that simplify serverless deployment. The layer is actually a zip file that contains all the dependencies. It shrinks down the size of the deployment package and makes your deployment more robust. In the next part, we will progress with this option.

Create Layer

To start, open the Lambda service by visiting the Lambda Home page https://console.aws.amazon.com/lambda/home#/layers.

You will see a list of your available layers.

Lambda Layers

Press Create Layer and you will see the form as on an image:

Lambda Create Layer

On the form, we will need to add certain configurations:

You can also download a zen engine python layer to your machine and manually upload it.

Press Create and your Lambda layer will be created.

Lambda Layer - created

Create a Function and attach Layer

Next, we will need to create a Function and attach Layer.

On the left side of Navigation select Functions and a new page will appear with a list of available functions.

Lambda Functions

To create a new function select Create function.

Next, on the configuration page:

Lambda Function Create

Your Runtime and Architecture must match with the previously created Layer.

Next press Create Function and your Lambda Function will be created!

Lambda Function

Now we will need to attach our reusable Layer with built-in dependencies. On the Lambda page scroll to the bottom and in the Layers section press Add a Layer.

Lambda Function - Add a Layer

The new form will appear:

Lambda Function Layer

On the form select Custom Layers, from the dropdown select your previously created layer (e.g. zen_engine_python_layer), and select a version. Press Add and your new Layer will be added to your Lambda Function.

Your Lambda will now have a Layer with dependencies attached.

Configure permissions

Next, we will need to allow Lambda to access S3. To do this go to a Configuration Tab, open the Permissions side menu, and press Edit.

Lambda Permissions

On the newly opened form scroll to the bottom and tick Create a new role from AWS policy templates in the Execution role section.

Enter a new name, for example, bre_python_lambda, and in the Policy Template search and select Amazon S3 object read-only permissions.

Lambda Permission

Press Save and your new Role will be added to Lambda.

Python Rules Engine

As we have all dependencies and permissions set, we are now left with writing Python code.

Python Business Rules Engine

For the source code please visit: https://github.com/gorules/zen/blob/master/examples/python-lambda-s3/lambda_function.py

Because we have added a lambda layer we are able to import the zen library without having any dependency directly in the function.

The code that you are seeing here is using the loaders logic which is made to read decision files directly from Amazon S3 based on a key property of the request.

Create a JDM file

To create and manage decision files we can either create JSON files manually or use GoRules free editor.

Open the editor by clicking on the Editor. The link is loaded with the pre-defined template for the basic evaluation of shipping fees. To learn more about JDM Graphs please visit JSON Decision Model (JDM) documentation. You can also open the decision table by clicking the Open link in the Fees node of the graph.

GoRules JDM

Next, download a JSON file by pressing File > Download JSON and rename a file to shipping-fees.json.

Upload a decision file to your S3 bucket.

Amazon S3 - upload file

Test Rules Engine

Now, we only need to test the Lambda function! Go back to your Lambda Function and press Test.

Lambda Test

A new dialog will appear prompting the configuration of the test events. Select Create a new event, name it to test, and for the event JSON paste:

Lambda Test JSON

The key is the file name that we have uploaded to our Amazon S3 bucket and Context is input data that will be validated in the rules engine.

After saving a new test event press Test again. You can finally see a result.

Business Rules Engine Performance

Likewise, you are able to upload many different decision files and execute them by naming a Key in the JSON event to a new file.

Summary

In this blog, we explored how to build a serverless rules engine using Python and AWS Lambda. We started by creating an Amazon S3 bucket to store our data. Next, we created a Lambda function and attached a Zen Engine layer, which is a pre-built dependency, to our function. This layer helps streamline the deployment process and makes our Lambda function more robust. We also configured permissions to allow the Lambda function to access S3, ensuring that it has read-only access to the necessary objects.

In conclusion, building a Python rules engine with AWS Lambda and S3 can offer numerous benefits for businesses, including increased agility, improved data processing capabilities, and cost savings. By following the steps outlined in this blog, developers can easily set up a serverless rules engine and leverage the power of serverless computing for their business needs.