How to Schedule a VSI Power On and Off Using IBM Cloud Functions

2 min read

By: Gerson Itiro Hidaka

Integrating the IBM Cloud Functions Periodic Triggers and Actions to schedule the VSI power on and power off.

We previously announced the suspend billing capabilities for Virtual Server Infrastructure (VSI) that are helping customer to pay only for what they use. In addition to this, we can integrate the IBM Cloud Functions Periodic Triggers and Actions to schedule the VSI power on and power off.

Requirements

Creating the Functions Action

The following steps will help you to create an Action on the IBM Cloud Functions. This Action is based on a Python 2 script and can be used to both power the VSI on or power it off. 

  1. Clone this repo using the "git clone" command and access the app folder:
    git clone https://github.com/itirohidaka/PowerOff-Functions.git
    cd PowerOff-Functions
  2. Modify the __main__.py file, replacing the <username> with the SoftLayer UserName and <apikey> with the API Key. To get the API keys for Softlayer, please visit Your Softlayer Profile under “API Access Information.”

    username = '<username>'
    key = '<apikey>'
    Figure 1: Inserting the username and API key on __main__.py file.

    Figure 1: Inserting the username and API key on __main__.py file.

  3. Create the Python virtualenv using a docker command (no need to modify, just copy and paste on the terminal.

    docker run --rm -v "$PWD:/tmp" openwhisk/python2action bash -c "cd /tmp && virtualenv virtualenv && source virtualenv/bin/activate && pip install -r requirements.txt"
  4. Create the zip file with virtualenv folder and __main__.py file
    zip -r <zip_file> <virtualenv_folder> <main_file>

     Example:

    zip -r hello.zip virtualenv __main__.py
  5. Push the zip "package" to IBM Cloud Functions:

    ibmcloud fn action create <action_name> <zip_file> --kind

    Example:

    ibmcloud fn action create itiroaction01 hello.zip --kind python:2

Creating the Functions Periodic Trigger

A trigger is a declaration that you want to react to a certain type of event, whether from a user or an event source. Triggers fire when they receive an event. In our case, we are using the Period events to fire the Trigger.

  1. Open the IBM Cloud console.
  2. Log in to the IBM Cloud Console using your credentials (username and password).
  3. Click on the three-line menu (hamburger menu) and click on Functions.
  4. Click on Actions and check if you Action appear on the list. In my example, I'm using "itiroaction01" for the action name.
  5. Click on Trigger and click on the Create button to create a new Function Trigger.
  6. Click on Create Trigger.
  7. Click on Periodic.
    Figure 2: Choosing the Periodic Trigger.

    Figure 2: Choosing the Periodic Trigger.

  8. Type the name in the Trigger Name field.

  9. Select the date, days, and hours that the action will be executed. You can select a predefined period in the Select pattern field:

    Figure 3: Configuring the New Trigger.

    Figure 3: Configuring the New Trigger.

  10. In the JSON Payload field, type:

    {
      "vsiname":"<name_of_the_vsi>",
      "poweraction":"<power_action>"
    }

    Example:

    {
      "vsiname":"virtualserver01",
      "poweraction":"on"
    }

    OBS: Change <name_of_the_vsi> brackets with the name of the VSI that needs to be powered on/off.

  11. Click the Create button.

  12. On the next screen, click the Add button to associate an action with the Trigger.

  13. Click the Select Existing button:  

    Figure 4: Selecting the Action.

    Figure 4: Selecting the Action.

  14. Click on the Select an Action field and select your action.

  15. Click the Add button.

  16. Your Function is now created with a Trigger and an Action to power on a VSI named "virtualserver01".

You can now use the same example to create another Trigger to power off a VSI. To do that, just change the value of "poweraction" to "off" in Step 10.

References

Be the first to hear about news, product updates, and innovation from IBM Cloud