This is a hands-on workshop walking through creating and using IBM Cloud Functions to process video files stored in IBM Cloud Object Storage. This workshop was originally created for the Tweakers group in the Netherlands.
- You will need an IBM Cloud account, which you can get for free signing up here: https://ibm.biz/ibm-cloud-functions-account-reg
- There are two ways to access the CLI for IBM Cloud:
- You can install the IBM Cloud CLI locally: https://www.ibm.com/cloud/cli or
- You can use the IBM Cloud Shell in your web browser: https://cloud.ibm.com/shell (ensure you change the location to
Frankfurt
if not already there)
- log into IBM Cloud via the CLI and target the default group:
$ ibmcloud login --sso
$ ibmcloud target -g Default --cf
- (If installing locally) Install the cloud functions and object storage plugins
$ ibmcloud plugin install cloud-functions
$ ibmcloud plugin install cloud-object-storage
-
From the IBM Cloud homepage, click on the catalog and choose 'Cloud Object Storage'
-
Create a bucket that we will need for this workshop, give each bucket a name, and ensure that the resilience is set to regional and set
eu-de
as the region. -
Repeat the step above again to create a second bucket.
-
Create some service credentials for us to use later in our cloud functions. Click on "service credentials" on the left
-
Click blue "New credential" button top right of screen. The default name and role (writer) are good. Ensure that you enable "Include HMAC Credential"
-
Click on 'Cloud Object Storage' to create a Trigger for our COS instance:
-
Fill in the details of the authorization grant. The Source Serive is
Functions
, the Source service instance in the namespace you createdtweakers
. The target service isCloud Object Storage
and the Service instance the name of the COS instance you created earlier. Select thenotifications manager
checkbox at the bottom and click 'Create' -
Refresh our triggers page and you should now be able to give the trigger a name
cos_trigger
and the COS instance should be selected. Ensure your first bucket is selected. ClickCreate
-
Give your namespace the name
tweakers
and choose the location, in this case we will useLondon
to match our Object Storage. -
Click on 'Start Creating' on the IBM Cloud Functions homepage
-
Name your action
hello_world
and pick a runtime, in this casepython 3.7
: -
You will be taken to a page in which you can edit code yourself and click 'Invoke' to run it
-
Actions take a dictionary parameter in which arguments to the function are marshalled. You can change the code to take a name, e.g.:
import sys def main(dict): name = dict.get('name', 'Random Bob') return { 'message': f'Hello {name}'}
-
Click on 'Invoke with parameters' to set the parameters for the call
-
First we need to target the Default resource group
% https://cloud.ibm.com/shell
-
First we need to target the
tweakers
namespace% ibmcloud fn namespace target tweakers ok: whisk namespace set to tweakers
-
We can list our functions, we should see the one we just created:
% ibmcloud fn action list actions /25ec8f7a-8e10-422a-94bf-7e7f8d7d8fd9/hello_world private python:3.7
-
We can invoke our action from the CLI:
% ibmcloud fn action invoke hello_world --result { "message": "Hello Random Bob" } % ibmcloud fn action invoke hello_world --result --param name Matt { "message": "Hello Matt" }
-
We can also list our trigger we created at the start:
% ic fn trigger list triggers /25ec8f7a-8e10-422a-94bf-7e7f8d7d8fd9/cos_trigger private
-
Download the example python code to convert a movie to a sound file:
% curl https://raw.githubusercontent.com/IBMDeveloperUK/Hands-on-IBM-Cloud-Functions/main/convert_format.py -o convert_format.py
-
Create a package for our functions called
tweakers
in which we will set various default parameters% ic fn package create tweakers --param geo eu-de --param endpoint s3.private.eu-de.cloud-object-storage.appdomain.cloud
-
Bind the service credentials from our COS instance to the package so our function can read/write to our COS buckets
% ibmcloud fn service bind cloud-object-storage tweakers --instance "Cloud Object Storage-pb"
-
Create our action. This action uses a docker image, that has
ffmpeg
installed. We set the parameters for our buckets as above. Change them for the names you created. We also give it 2GB of RAM and a 10 minute timeout in case we want to process large files.% ic fn action update tweakers/convert_format convert_format.py --docker choirless/choirless_py_actions:release-0.23 --timeout 600000 --memory 2048 --param bucket1 tweakers-matt-1 --param bucket2 tweakers-matt-2
-
Upload a test movie to our first cost bucket (
tweakers-matt-1
in my case) by dragging and dropping it in the bucket view in the web -
Test our function manually. Give it the name of the file you uploaded before
% ic fn action invoke convert_format --param key matt.mkv --result
-
Check in the second COS bucket, you should see a
.wav
file. This is our converted file. -
To hook the trigger up to the action so it fires automatically, you need to create a rule to link the two:
% ic fn rule create bucket_upload_rule cos_trigger tweakers/convert_format
-
Test it by dragging another file (or the same again) to the first COS bucket. You will see the new converted file appear in the second bucket. You can also see the activations:
% ic fn activation list Datetime Activation ID Kind Start Duration Status Entity 2020-12-14 17:11:00 09cac1a2fac748de8ac1a2fac7d8dea8 blackbox warm 649ms success 323b2dbb-5...6f7915f/convert_format:0.0.1 2020-12-14 17:11:00 02ace321c40841fcace321c408a1fc40 unknown warm 0s success 323b2dbb-5...6f7915f/cos_trigger:0.0.1