Getting Started with IBM Cloud Functions using the Bluemix CLI
Deploying and invoking a Cloud Function on IBM Bluemix using the Bluemix CLI
What will we build?
We’re going to build a very simple API for fetching metadata from websites. Users should be able to call our API through HTTP and pass a url parameter.
Step 1: Create a Bluemix account
In the IBM Bluemix Console and make sure you know which region your account is bound to (if your account is in the free tier).
Step 2: Install the IBM Cloud CLI
The steps to install the Command Line Interface for IBM cloud services are very straight forward.
Up-To-Date instructions from the IBM website
Step 3: Authenticate using the CLI
My resources are based in the London region, so in the examples I will be authenticating the following API endpoint:
api.eu-gb.bluemix.net
Refer to this page to find the API endpoint for your region.
Authenticating is done through the following command:
$ ibmcloud login -a <YOUR ENDPOINT>
So in my case:
$ ibmcloud login -a api.eu-gb.bluemix.net
You will be prompted your bluemix e-mail address and password. That’s it!
Step 4: Getting ready
When you have successfully authenticated you can install the cloud-functions plugin.
$ ibmcloud plugin install cloud-functions
Run the command below to have your ibmcloud commands targeting the correct organisation.
$ bx target —-cf
If this last command doesn’t work you will have to read up on configuring your account. I have seen examples of this, and the solution is not generic, it depends on the configuration of your account in some way probably. This is a good starting point for reading up on this.
Step 5: Writing and deploying your first function
Our workflow will be as follows: We will create a folder and file structure ourselves to manager our code (and repos) and use the Bluemix CLI to deploy the code to the cloud.
Get Rene van Pelt’s stories in your inbox
Join Medium for free to get updates from this writer.
I will start by creating a directory and a file for our first function.
$ mkdir hello_world && cd hello_world
$ touch hello_world.js
Now edit the contents of hello_world/hello_world.js to:
And deploy the function with the following command:
$ ibmcloud fn action create hello_world hello_world.js
And you should see this:
This means that the code in the file hello_world/hello_world.js (within the main function) is associated with an action called hello_world in your IBM Bluemix account.
Execute the following command to see a list of all deployed actions in your IBM Bluemix account:
$ ibmcloud fn action list
Step 6: Run the code! I mean.. Call the Action!
Since the IBM Bluemix CLI executes commands within the context of your Bluemix account, you can directly call cloud functions manually using the following command:
$ ibmcloud fn action invoke —-blocking hello_world
The blocking flag means that your terminal will wait until the function is executed or timed out. This is needed to actually see the response. You should see a JSON response containing our {payload: ‘Hello world’} response.
Step 7: Updating Cloud Functions to use Parameters
Now we will update our code and update the corresponding cloud action. We start by updating hello_world/hello_world.js to this:
Run the following commands to update and run the cloud action:
$ ibmcloud fn action update hello_world hello_world.js
$ ibmcloud fn action invoke —-blocking hello_world
Note that we’ve used the second command before.
And you should see the exact same result as before since we didn’t pass any parameters.
Now we are going to invoke the hello_world with a name parameter using this command
$ ibmcloud fn action invoke --blocking hello_world --param name ‘Nick'
Which should yield a response like this: {payload: ‘Hello Nick’}
That’s it for now! Follow me on Medium, Twitter and/or LinkedIn to get a notification when I publish the next part.






