July 17, 2019 By Gerson Itiro Hidaka 3 min read

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. 

NOTE: For VPC/Code Engine deployments, see “Using IBM Cloud Code Engine to Turn Virtual Server Instances On/Off in an Automated/Scheduled Manner.”

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.

  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.

  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.

  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.

  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

Was this article helpful?
YesNo

More from Cloud

Serverless vs. microservices: Which architecture is best for your business?

7 min read - When enterprises need to build an application, one of the most important decisions their leaders must make is what kind of software development to use. While there are many software architectures to choose from, serverless and microservices architectures are increasingly popular due to their scalability, flexibility and performance. Also, with spending on cloud services expected to double in the next four years, both serverless and microservices instances should grow rapidly since they are widely used in cloud computing environments. While…

Serverless use cases: How enterprises are using the technology to let developers innovate

6 min read - Serverless, or serverless computing, is an approach to software development that empowers developers to build and run application code without having to worry about maintenance tasks like installing software updates, security, monitoring and more. With the rise of cloud computing, serverless has become a popular tool for organizations looking to give developers more time to write and deploy code. Despite its name, a serverless framework doesn’t mean computing without servers. In a serverless architecture, a cloud service provider (CSP) handles…

How a US bank modernized its mainframe applications with IBM Consulting and Microsoft Azure

9 min read - As organizations strive to stay ahead of the curve in today's fast-paced digital landscape, mainframe application modernization has emerged as a critical component of any digital transformation strategy. In this blog, we'll discuss the example of a fictional US bank which embarked on a journey to modernize its mainframe applications. This strategic project has helped it to transform into a more modern, flexible and agile business. In looking at the ways in which it approached the problem, you’ll gain insights…

IBM Newsletters

Get our newsletters and topic updates that deliver the latest thought leadership and insights on emerging trends.
Subscribe now More newsletters