IBM Cloud Functions Adds Support for Cloud Object Storage Triggers

2 min read

By: Mark Deuser

What’s new with IBM Cloud Functions and Cloud Object Storage triggers?

Starting today in the US-South region, IBM Cloud Functions allows you to create triggers based on Cloud Object Storage bucket changes. With the new /whisk.system/cos-experimental package, you can create triggers and rules to fire actions when bucket objects are created, modified, or deleted. This package is currently an experimental offering. You can try out this new feature and provide feedback related to desired feature enhancements or problems you encounter.

A new Cloud Object Storage package

The existing /whisk.system/cloud-object-storage package is joined by the /whisk.system/cos-experimental package. The former package continues to provide convenient actions you can use to access your Cloud Object Storage instance, while the cos-experimental package introduces a feed action that is used during trigger creation to configure bucket-specific events.

Let’s put this to work

Before starting, you must have a Cloud Object Storage service instance created and running in the IBM Cloud. This instance should have at least one bucket as well as a service credential containing the API key used to access your storage. You should also know the endpoint that was used to create the bucket; Cloud Functions will use this endpoint to determine bucket changes.

  1. Create a package binding:
    ibmcloud fn package bind /whisk.system/cos-experimental myCosPkg
  2. Bind your Cloud Object Storage credentials to your package binding:
    ibmcloud fn service bind cloud-object-storage myCosPkg
  3. Create a trigger named cosTrigger with the changes feed in the package binding that you created. Use your bucket name and IBM Cloud Object Storage endpoint parameter values:
    ibmcloud fn trigger create cosTrigger --feed myCosPkg/changes \
    --param bucket myBucket \
    --param endpoint s3.us-south.cloud-object-storage.appdomain.cloud
    Example output:
    ok: created trigger feed cosTrigger
  4. Create an action to verify that the trigger, the change feed, and the rule are all configured and working correctly. Save the following JavaScript code as cosChange.js.
    function main(data) {
       console.log(data);
    }
  5. Create an action called cosChange by using the cosChange.js code.

    ibmcloud fn action create cosChange <filepath>/cosChange.js
  6. Create a rule to connect the cosChange action to the cosTrigger trigger.

    ibmcloud fn rule create cosRule cosTrigger cosChange
  7. In a separate terminal window, start polling for activations to give clear visibility of what is happening. When the trigger fires and the action is run, this command lists the activation records for each of these operations as they occur. 

    ibmcloud fn activation poll
  8. In your IBM Cloud Object Storage dashboard, either modify an existing bucket object or create one. To learn how to add an object to your bucket, see Add some objects to your bucket. For each bucket object change, observe new activations for the cosTrigger trigger and cosChange action. If you are unable to observe new activations, verify that the apikey, endpoint, and bucket parameter values are correct. 

    ibmcloud fn trigger get cosTrigger

Going further

The cos-experimental event provider also easily integrates with the existing actions defined in the cloud-object-storage package; you can create rules that run those actions when a bucket is updated. For more information about this feature, refer to the IBM Cloud Functions Documentation.

For an overview of object storage, see "Object Storage: A Complete Guide."

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