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

IBM Cloud Reference Architectures unleashed

2 min read - The ability to onboard workloads to cloud quickly and seamlessly is paramount to accelerate enterprises digital transformation journey. At IBM Cloud, we're thrilled to introduce the IBM Cloud® Reference Architectures designed to empower clients, technical architects, strategists and partners to revolutionize the way businesses harness the power of the cloud. VPC resiliency: Strengthening your foundation Explore the resilience of IBM Cloud Virtual Private Cloud through our comprehensive resources. Dive into our VPC Resiliency white paper, a blueprint for building robust…

Enhance your data security posture with a no-code approach to application-level encryption

4 min read - Data is the lifeblood of every organization. As your organization’s data footprint expands across the clouds and between your own business lines to drive value, it is essential to secure data at all stages of the cloud adoption and throughout the data lifecycle. While there are different mechanisms available to encrypt data throughout its lifecycle (in transit, at rest and in use), application-level encryption (ALE) provides an additional layer of protection by encrypting data at its source. ALE can enhance…

Attention new clients: exciting financial incentives for VMware Cloud Foundation on IBM Cloud

4 min read - New client specials: Get up to 50% off when you commit to a 1- or 3-year term contract on new VCF-as-a-Service offerings, plus an additional value of up to USD 200K in credits through 30 June 2025 when you migrate your VMware workloads to IBM Cloud®.1 Low starting prices: On-demand VCF-as-a-Service deployments begin under USD 200 per month.2 The IBM Cloud benefit: See the potential for a 201%3 return on investment (ROI) over 3 years with reduced downtime, cost and…

IBM Newsletters

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