November 16, 2017 | Written by: Ashher Syed and Jesse Alva
Categorized: Compute Services | DevOps
Share this post:
Introducing a new command for the IBM Cloud Functions CLI plug-in to make it easier to configure your actions to have access to your IBM Cloud service credentials. The new service command is a convenient way to make your IBM Cloud service credentials available to your Cloud Functions code at run time. The new command helps with a form of secret management for your IBM Cloud services within the context of IBM Cloud Functions.
Introducing the bx wsk service CLI command
The bx wsk service bind retrieves the specified service’s credentials and “binds” the requested service credentials to your Cloud Functions action as a new parameter. Once the credentials are “bound”, you can leverage them like any other parameter that is defined for that action.
The new service command is not to be confused with the cf bind-service command that is available in Cloud Foundry. It is simply an automated way to create a new parameter on your existing action that contains service credentials.
How the service command differs from the existing package refresh command
If you are familiar with IBM Cloud Functions packages, you might wonder what the difference between bx wsk service bind and bx wsk package refresh is. The package refresh command only updates credentials for very specific IBM provided service for which Cloud Functions provides custom integrations. For example, Cloudant, MessageHub and the Weather services are supported.
The service bind command is more flexible and allows you to bind any IBM Cloud service to any action that is defined in IBM Cloud Functions. The only caveat is that you must have credentials defined for the service that you want to bind.
To obtain the credentials from your Watson Conversation service, and make it available to your “TravelAdvisor” action, run the following command:
bx wsk service bind conversation TravelAdvisor
To see what is currently bound and where it came from, you can run the following command:
bx wsk action get TravelAdvisor
The Watson conversation service credentials are now available to the TravelAdvisor action as a parameter named __bx_creds as shown in the following code. For example, pretend the TravelAdvisor action has the following code:
console.log("Use the Watson Conversation API with credentials:", url, username, password);
If you run bx wsk action invoke TravelAdvisor -r, you might see something like the following output:
What gives? Well the output was printed to standard out, and not returned as part of the response. To view the log, you can check the last activation with the command bx wsk activation get -l :
ok: got activation&amp;nbsp;abcde……xyz
"2017-10-31T15:14:51.08598289Z stdout: Use the Watson Conversation
API with credentials: <watson-url> <watson-username> <watson-password>"
If you no longer wish to have this binding reflected in the action, as you no longer use this service, you can remove it using the unbind command:
bx wsk service unbind conversation TravelAdvisor
If you rerun your TravelAdvisor action again (bx wsk action invoke TravelAdvisor) after your credentials are unbound, you might see something like the following output:
"error": "An error has occurred: TypeError: Cannot match against ‘undefined’ or ‘null’."
Remember to always run the bx wsk service command after you set up all your other default parameters.If you run bx wsk action update to modify parameters, the credentials binding is removed along with any other parameters not included in the action update. If this happens, remember to run bx wsk service bind again.
For more information about this new feature, take a look at the documentation for the Cloud Functions plug-in – https://console.bluemix.net/docs/openwhisk/bluemix_cli.html#cli_bind
Get started with IBM Cloud Functions with your Free account.