Trigger a function by Object Storage

Automate OCI Functions with Object Storage events. Learn event-driven serverless architecture using Terraform IaC on Oracle Cloud.
Trigger a function by Object Storage
Trigger a function by Object Storage

The OCI Function service allows you to run code on infrastructure that you don’t have to manage. In this link I presented a basic example of an OCI function developed in Python.

A very interesting feature is that functions can be automatically invoked by other services through an event, so we can also execute code in response to an action in the OCI cloud, or use a function as a link between multiple cloud services that can form a complex workload.

In this case we will see how to invoke a function via an event triggered by another OCI service, in the specific when we copy a file to an Object Storage.

Given the increase in infrastructure complexity we would also introduce a new concept called IaC to facilitate the deployment of all configurations in the cloud, specifically we will use a template Terraform , widely supported on OCI.

So let’s run the two commands to create the infrastructure on OCI

terraform init
terraform apply

Once the infrastructure has been created, we can test the execution of the func.py function by inserting a file inside the bucket.

echo "Hello world!" > hello.txt
oci os object put --bucket-name test --file hello.txt

Once the file has been copied, the function will then be activated and through the logs we could verify the execution and the output

Function log

This fairly trivial example can help you to understand how through events you can create automatisms and in this case access files in a dynamic and scalable way.

In more complex scenarios it will be possible to integrate other OCI services to process or extract data from documents saved in a Bucket and save them, for example, in other services such as Autonomous DB.

Once you have finished your tests it will be possible to delete the infrastructure with terraform

terraform destroy

Frequently Asked Questions

How do I trigger an OCI Function from an Object Storage event?

You create an OCI Event Rule that filters on Object Storage events (such as 'Object Created') and sets the action to invoke a Function. When a file is uploaded to the bucket, the event fires and passes object metadata to the function.

What OCI services can trigger a Function via events?

OCI Events supports triggers from Object Storage, Block Volumes, Compute, Networking, Identity, Database, and many other services. Any resource that emits OCI Events can be linked to a Function action.

Can I provision event-driven OCI Function triggers with Terraform?

Yes. You can define OCI Event Rules, Function applications, and Function resources entirely in Terraform using the OCI provider. This enables fully reproducible, version-controlled event-driven architectures.

Related posts