Compute Services

Simplify binding your IBM Cloud services to serverless Functions

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.

Examples

To obtain the credentials from your Watson Conversation service, and make it available to your “TravelAdvisor” action, run the following command:

[code]
bx wsk service bind conversation TravelAdvisor
[/code]

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

[code]
bx wsk action get TravelAdvisor
[/code]

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:

[code]function main({
message,
__bx_creds: {conversation:{url}},
__bx_creds: {conversation:{username}},
__bx_creds: {conversation:{password}}
}){
console.log("Use the Watson Conversation API with credentials:", url, username, password);
}
[/code]

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

[code]
{ }
[/code]

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 :

[code]
ok: got activation abcde……xyz

{
"namespace": "jalva@us.ibm.com_dev",
"name": "TravelAdvisor",
"version": "0.0.3",
"subject": "jalva@us.ibm.com",
"activationId": "b96d80816f0349fbad80816f03e9fbdf",
"start": 1509462891021,
"end": 1509462891087,
"duration": 66,
"response": {
"status": "success",
"statusCode": 0,
"success": true,
"result": {}
},
"logs": [
"2017-10-31T15:14:51.08598289Z stdout: Use the Watson Conversation
API with credentials: <watson-url> <watson-username> <watson-password>"
],
"annotations": [
{
"key": "limits",
"value": {
"logs": 10,
"memory": 256,
"timeout": 60000
}
},
{
"key": "path",
"value": "jalva@us.ibm.com_dev/TravelAdvisor"
},
{
"key": "kind",
"value": "nodejs:6"
}
],
"publish": false
}
[/code]

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:

[code]
bx wsk service unbind conversation TravelAdvisor
[/code]

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:

[code]
{
"error": "An error has occurred: TypeError: Cannot match against ‘undefined’ or ‘null’."
}
[/code]

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.

Product Marketing Manager - Cloud Computing

More Compute Services stories
October 16, 2018

IBM Cloud Kubernetes Service: Deployment Patterns for Maximizing Throughput and Availability

With the upcoming release of Kubernetes version 1.12 on IBM Cloud Kubernetes Service, we are releasing the new IKS LoadBalancer 2.0 for public beta so that customers may test. This article discusses the capabilities of this LoadBalancer service and a few deployment patterns around it, providing examples along the way.

Continue reading

October 10, 2018

Kubernetes API Server Log Collection

With the latest IBM Cloud Kubernetes Service CLI plug-in, you can collect your Kubernetes API server logs and drop them in an IBM Cloud Object Storage (COS) bucket. These API server logs are an invaluable resource because they record every request that passes through the Kubernetes API server.

Continue reading

October 9, 2018

IBM Cloud Foundry Enterprise Environment: Real-World Usage

The Cloud Foundry Enterprise Environment (CFEE) offering provides key security isolation for application hosting for critical regulatory requirements, geo-location restrictions, and other important reasons. Check out some real-world examples of applications deployed on the CFEE.

Continue reading