Simplify binding your IBM Cloud services to serverless Functions

5 min read

Simplify binding your IBM Cloud services to serverless Functions

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<br>


To see what is currently bound and where it came from, you can run the following command:

bx wsk action get TravelAdvisor<br>


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:

function main({<br>
__bx_creds: {conversation:{url}},<br>
__bx_creds: {conversation:{username}},<br>
__bx_creds: {conversation:{password}}<br>
console.log("Use the Watson Conversation API with credentials:", url, username, password);<br>

If you run bx wsk action invoke TravelAdvisor -r, you might see something like the following output:

{ }<br>


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;amp;nbsp;<p></p>
"namespace": "",<br>
"name": "TravelAdvisor",<br>
"version": "0.0.3",<br>
"subject": "",<br>
"activationId": "b96d80816f0349fbad80816f03e9fbdf",<br>
"start": 1509462891021,<br>
"end": 1509462891087,<br>
"duration": 66,<br>
"response": {<br>
"status": "success",<br>
"statusCode": 0,<br>
"success": true,<br>
"result": {}<br>
"logs": [<br>
"2017-10-31T15:14:51.08598289Z stdout: Use the Watson Conversation<br>
API with credentials: <watson-url> <watson-username> <watson-password>"<br>
"annotations": [<br>
"key": "limits",<br>
"value": {<br>
"logs": 10,<br>
"memory": 256,<br>
"timeout": 60000<br>
"key": "path",<br>
"value": ""<br>
"key": "kind",<br>
"value": "nodejs:6"<br>
"publish": false<br>


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<br>


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'."<br>

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 –

Get started with IBM Cloud Functions with your Free account.

Be the first to hear about news, product updates, and innovation from IBM Cloud