When your Visual Builder applications have predictable and steady usage, you can set a fixed number of nodes for your VB instance. In the rare case when the load changes, you manually update the number of nodes as needed.

On the other hand, in cases where the load fluctuates, having an automated way to scale the service would be nice.

The following guide shows how to setup an autoscaler for Visual Builder.  It can be adapted for other Oracle Cloud services.

Resources Used

The following Oracle Cloud resources are used in this guide.

  • Developer Services
    • Visual Builder
      • Visual Builder
      • Visual Builder Studio
    • Containers & Artifacts
      • Container Registry
    • Functions
      • Applications
    • Application Integration
      • Notifications
  • Observability & Management
    • Monitoring
      • Alarm Definitions

Please review the pricing for each service and ensure you have the correct permissions before implementing them.

Overview

  1. You will update your Visual Builder instance with a Python application using the Oracle Cloud SDK for Python
  2. The application will be deployed as an Oracle Cloud Infrastructure Function.
  3. The function will be executed by an Oracle Cloud Notification.
  4. The notification will be called by an Oracle Cloud Alarm that will fire whenever your Visual Builder instance OCPU usage exceeds the defined threshold.
    When the alarm fires it will pass 3 pieces of information to the Python application.
    1. The number of nodes to scale the instance.
      The example below passes a 1 to scale the instance up, but you could modify it to send a -1 to scale the instance down.
    2. The OCID of the Visual Builder instance.
    3. The type of the alarm.  Either "OK_TO_FIRING" meaning the alarm is now triggering, or "FIRING_TO_OK" meaning the alarm is resetting.

There are many other ways you could configure the code and components used in this post.  These examples are intended to be very basic for example purposes only.

This guide is meant as an example only and should NOT be used in a production system.

Create a Python application

The method below will process information sent from the monitoring alarm and scale your instance as requested.  There is a built in safety catch that will not allow your instance to be scaled above 3 nodes.

Review the code, to make sure you understand everything it's doing before you deploy it.

Executing this code could add charges to your Oracle Cloud account!

Create a Git repository in Visual Builder Studio

  1. Log into Visual Builder Studio.
  2. Create a new git repository.
    Create Git Repo
  3. Enter a name.
    'manage-vb'
  4. Enter a description.
    'Fn function used to scale a Visual Builder instance'
  5. Click "Create".
    Repository Details

Add files to your Git Repository

  1. Create a new file.
    Create a new file
  2. Name the file.
    'requirements.txt'
  3. Enter the file contents.
    fdk
    oci
  4. Commit your changes.
    Commit requirements file
    Commit Requirements Details
  5. Go back to the repository root.
    Return to root
  6. Create a new file.
    New File
  7. Name the file.
    'func.py'
  8. Enter the file contents.
    import io
    import json
    import oci
    
    from fdk import response
    
    def handler(ctx, data: io.BytesIO = None):
        resp = {"errors": {}, "messages": {}}
    
        try:
            # Parse incoming alarm body
            try:
                alarm_type = json.loads(data.getvalue())["type"]
                data = json.loads(data.getvalue())["body"].split()
    
                node_change = int(data[0])
                vb_ocid = data[1]
    
                resp["messages"]["parse_body"] = {"data": data}
            except Exception as ex:
                resp["errors"]["parse_body"] = {"data": data, "exception": str(ex)}
                raise
            if alarm_type == "OK_TO_FIRING":
                # OCI objects and information
                try:
                    signer = oci.auth.signers.get_resource_principals_signer()
                    vb_client = oci.visual_builder.VbInstanceClient(config={}, signer=signer)
    
                    new_nodes = vb_client.get_vb_instance(vb_ocid).data.node_count + node_change
    
                    resp["messages"]["oci_objects"] = {"new_nodes": new_nodes}
                except Exception as ex:
                    resp["errors"]["oci_objects"] = {"new_nodes": new_nodes, "exception": str(ex)}
                    raise
    
                if 0 < new_nodes < 4:
                    # Scale the instance
                    try:
                        oci_resp = vb_client.update_vb_instance(
                            vb_instance_id=vb_ocid,
                            update_vb_instance_details=oci.visual_builder.models.UpdateVbInstanceDetails(
                                node_count=new_nodes
                            ),
                        )
    
                        resp["messages"]["scale_instance"] = 'Work Request submitted to set the node count for instance {} to {}.'.format(vb_ocid, new_nodes)
                    except Exception as ex:
                        resp["errors"]["scale_instance"] = {"exception": str(ex)}
                        raise
                else:
                    resp["errors"]["scale_instance"] = 'Instance {} cannot be scaled to {} nodes.'.format(vb_ocid, new_nodes)
            else:
                resp = {"Alarm Type": alarm_type}
    
        except Exception as ex:
            print(resp)
            print(str(ex), flush=True)
    
        return response.Response(
            ctx,
            response_data=resp,
            headers={"Content-Type": "application/json"},
        )
  9. Commit your changes.
    Commit Func File
    Commit Func Details
  10. Copy the repository URL and save it for later.
    Copy Repo Url

Create an Oracle Cloud Repository

OCI Functions are deployed from an Image Repository.

This section will show you how to setup your own repository in Oracle Cloud.

  1. Under Developer Services, click "Container Registry".
    Open OCIR
  2. Click "Create Repository".
    Create Repository
  3. Select a compartment.
  4. Enter a Repository name.
    'vb-fn-apps'
  5. Click "Create".
    Repository Details
  6. When the repository details page opens save the repository Namespace for later.
    Repository Namespace

Create an Oracle Cloud Application

  1. In the Cloud Console, open the navigation menu under Developer Services, click "Applications".
    Open Applications
  2. Select the compartment you're using with OCI Functions, Click "Create Application".
    Create Application
  3. Enter a Name.
    'VB-Python-SDK'
  4. Choose your VCN.
  5. Select a Subnet.
  6. Set the instance Shape.
  7. Click "Create".
    Application Details

In addition to the information saved in the above steps, you will need to collect some information for the following steps.

  1. Under Identity & Security, click "Compartments".
    Open Compartments
  2. Locate the compartment containing your Application, hover the OCID and click "Copy".
  3. Do the same for your Container Repository if it's in a different compartment.
    Copy Compartment OCID

Your Application will need to be granted permission in order to make changes to your OCI objects.  You will create a Dynamic group for all of the applications in the compartment.  Then you will add a policy that allows members of that group to make changes to your OCI objects.

Check with your Cloud Administrator before adding the group and policy.

Create a Dynamic Group

  • In your cloud console under Identity and Security click "Dynamic Groups".
     OpenDynamic Groups
  • Click "Create Dynamic Group".
    Create Dynamic Group
  • Enter a name.
    'VB-FN'
  • Enter a description.
    'Function that will be used to scale VB instances'
  • Add a rule.
    ALL {resource.type = 'fnfunc', resource.compartment.id = '<Compartment containing your application>'}
  • Click "Create".
    Dynamic Group Details

Create a policy

  • In your cloud console under Identity and Security click "Policies".
    Open Policies
  • Click "Create Policy".
    Create Policy
  • Enter a Name.
    'vb-fn-policy'
  • Enter a description.
    'Allow function in the VB-FN dynamic group to manage Visual Builder instances in the <your compartment name> compartment'
  • Click the slider next to "Show manual editor".
  • Enter a policy statement.
    'Allow dynamic-group VB-FN to manage visualbuilder-instance in compartment <your compartment name>'
  • Click "Create".
    Policy Details

Deploy the Function code

You'll use the Oracle Cloud Shell to deploy the Function.  Oracle Cloud Shell has all of the necessary tools already installed and configured.

Prerequisites

 

  1. Open your Cloud Shell.
    Open Cloud Shell
  2. Setup some environment variables.
    export vn_application_compartment_ocid=<Application Compartment OCID from above>
    export container_repository_compartment_ocid=<Docker Repository Compartment OCID from above>
    export ocir_region_key=<Region key from above>
    export ocir_namespace=<Namespace from above>
    export application_name=VB-Python-SDK
    export function_name=manage-vb
    
  3. List the available contexts and select the context for your current region.
    fn list context 
    fn use context <your current region context>
    
  4. Configure the fn context.
    fn update context oracle.compartment-id ${vn_application_compartment_ocid}
    fn update context registry ${ocir_region_key}/${ocir_namespace}/${function_name}
    fn update context oracle.image-compartment-id ${container_repository_compartment_ocid}
    
  5. Clone your git repository using the URL you saved from above.
    git clone ssh://<your git repo from above>
    cd ${function_name}
    
  6. Initialize your code as an Fn function.
    fn init
    
  7. Commit and push the changes.
    git add .
    git commit -m"Initial Commit"
    git push origin main
    
  8. Deploy the function to your application.
    fn deploy --app ${application_name}

Test the Function

  1. In the Cloud Console, open the navigation menu under Developer Services, click "Visual Builder".
    Open Visual Builder
  2. Click on the instance you want to use.
    Note: this is the instance you'll be running tests on, so I would not recommend using a production instance.
    Select Vb Instance
  3. Copy the OCID and save it.
    Copy Vb Ocid
  4. Save the "Nodes" value to compare with after the tests.
    Visual Builder Nodes

The Python method accepts a JSON object from an OCI alarm.  In order to test the function, you'll use a JSON object with a body key.

{"body":"1 <Visual Builder OCID from above>", "type": "OK_TO_FIRING"}

The function has a built in safety stop at 3 nodes.  If your Visual Builder instance is at 3 or more nodes, you can change the '1' to a '-1'.  This will scale the instance down by 1 node.

Enter the following in your Cloud Shell.

echo '{"body":"1 <Visual Builder OCID from above>", "type": "OK_TO_FIRING"}' | fn invoke ${application_name} ${function_name}

You should receive a JSON object similar to this.

{
  "errors": {},
  "messages": {
    "parse_body": {
      "data": ["1", "ocid1.visualbuilderinstance.oc1.iad.myvbinstanceocid"]
    },
    "oci_objects": { "new_nodes": 2 },
    "scale_instance": "Work Request submitted to set the node count for instance ocid1.visualbuilderinstance.oc1.iad.myvbinstanceocid to 2."
  }
}

Switch back to your Visual Builder instance, your Nodes should now be 1 higher (or lower if you scaled down).

Edit your instance and set the nodes back to the original value.

Create a Notification

You will use a Notification to execute your Application Function.

  1. In your cloud console under Developer Services click "Notifications".
    Open Notifications
  2. Click "Create Topic".
    Create Topic
  3. Enter a Name.
    'ScaleVB'
  4. Click "Create".
    Topic Details
  5. Click on your new Topic.
    Open Topic
  6. Click "Create Subscription".
    Create Subscription
  7. Change the Protocol to "Function".
  8. Select your Function compartment.
  9. Select the application you created above.
  10. Select the function you created above.
  11. Click "Create".
    Subscription Details

Test your notification

  1. In your Notification detail page, click "Publish Message".
    Publish Message
  2. Enter the message body and click "Publish".
    {"body":"1 <Visual Builder OCID from above>", "type": "OK_TO_FIRING"}
    Message Details

Switch back to your Visual Builder instance, your Nodes should now be 1 higher (or lower if you scaled down).

Edit your instance and set the nodes back to the original value.

Create an alarm

  1. Open the details page for your Visual Builder instance.
  2. In the OCPU Consumption, look at the graph, choose a percentage that is in the middle of the fluctuations and save it.
    Ocpu Consumption
  3. Click "Options".
  4. Select "Create an alarm on this query".
    Create Alarm
  5. Enter an Alarm name.
    'HighUsage'
  6. Enter the Alarm body
    '1 ocid1.visualbuilderinstance.oc1.<OCID of your VB instance>'
    Alarm Details 1
  7. In the "Trigger rule" section set a value that is in the middle of the fluctuation you noted above.
    Trigger Rule
  8. In the "Define alarm notifications" section, under Destination select the Compartment and Topic you created above.
  9. Under Message Format, select "Send raw messages".
  10. Click "Save alarm".
    Alarm Details 2
  11. You should be in the Alarm Definitions page for your new alarm.
  12. Watch the Alarm data section, after a couple of minutes it will refresh.  When the value crosses your Trigger rule percentage the alarm will fire.
    Alarm Data Firing
  13. Look at the top of the alarm to see that your alarm is in a firing state.
    Alarm Firing
  14. Uncheck the "Alarm is enabled" checkbox so it will not continue to scale your instance!
    Warning! If you leave the alarm active it will continue to modify your nodes.  This can add charges to your cloud account.

Log into Visual Builder.

Watch the Nodes value. It should now be 1 higher (or lower if you scaled down).
Visual Builder Two Nodes

Edit your instance and set the nodes back to the original value.

Once you have this working and you want to auto scale the instance down, you could create an alarm calling the same function that triggers when you're below a certain threshold.  Just change the '1' to a '-1' in the alarm body and set a different trigger rule.

In this blog we walked you through setting up an automatic scaler for Visual Builder leveraging a collection of Oracle Cloud Infrastructure services and utilities. A similar approach could be adopted for other cloud services as well.