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
- Docker CE installed on your desktop/laptop
- Python 2.7 installed on your desktop/laptop for some local test
- SoftLayer SDK for Python installed on your desktop/laptop
- ibmcloud CLI installed on your desktop/laptop
- Functions Plugin installed on your desktop/laptop
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.
- Clone this repo using the “git clone” command and access the app folder:
-
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.”
-
Create the Python virtualenv using a docker command (no need to modify, just copy and paste on the terminal.
- Create the zip file with virtualenv folder and __main__.py file
Example:
-
Push the zip “package” to IBM Cloud Functions:
Example:
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.
- Open the IBM Cloud console.
- Log in to the IBM Cloud Console using your credentials (username and password).
- Click on the three-line menu (hamburger menu) and click on Functions.
- Click on Actions and check if you Action appear on the list. In my example, I’m using “itiroaction01” for the action name.
- Click on Trigger and click on the Create button to create a new Function Trigger.
- Click on Create Trigger.
- Click on Periodic.
-
Type the name in the Trigger Name field.
-
Select the date, days, and hours that the action will be executed. You can select a predefined period in the Select pattern field:
-
In the JSON Payload field, type:
Example:
OBS: Change <name_of_the_vsi> brackets with the name of the VSI that needs to be powered on/off.
-
Click the Create button.
-
On the next screen, click the Add button to associate an action with the Trigger.
-
Click the Select Existing button:
-
Click on the Select an Action field and select your action.
-
Click the Add button.
-
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.