Working with events in Oracle Cloud Infrastructure Part 1: service basics

aernesto24

Ernesto Lopez

Posted on January 11, 2022

Working with events in Oracle Cloud Infrastructure Part 1: service basics

Oracle Cloud Infrastructure Events is a service that allows you to create some automation base on change of state in a service, or maybe based in some sort of input received. Now, let get a step back, and Event is the occurrence of a particular situation. For this situations to be useful, events needs to be with some kind of information, for example, instance name, object name, status code, etc. The information can provide the automation something to work with.

Events are the base of serverless architecture, sometimes also referred to event-driven architecture. Additionally, events are crucial for contemporary software architectures as enables to decouple services, help to make software asynchronous calls and facilitate scaling processes.

An example of an event driven architecture:

  • You develop an image processing app.
  • Everytime a new image is uploaded to a bucket it triggers an event.
  • This event calls a function that grab that image and create a thumbnail from it.
  • The thumbnail is stored in another bucket.
  • This triggers another event that sends an email notification to the client.

NOTE OCI function is a serverless solution from OCI, that allows you to run a single purpose software without provisioning hardware or VMs, the software runs base on an event or a schedule job, it is like having functions as a service. More from Functions HERE

Getting back to OCI Events, some of the important characteristics are:


  • Event service use JSON object to define the event rules. You can think of a rule like the filter that will determine which events are important to consider, to determine what are going to be your inputs. Basically, Rules triggers actions. An example of a rule logic can be:
MATCH event WHERE (
  eventType EQUALS ANY OF (
  com.oraclecloud.computeapi.launchinstance.end
  )
)
Enter fullscreen mode Exit fullscreen mode

This event will trigger when a new compute instance is created

But...

Where is the JSON part??
On the event, itself, let see an example of an event:

{
  "eventType": "com.oraclecloud.computeapi.launchinstance.end",
  "cloudEventsVersion": "0.1",
  "eventTypeVersion": "2.0",
  "source": "ComputeApi",
  "eventTime": "2019-08-16T12:07:42.794Z",
  "contentType": "application/json",
  "data": {
    "compartmentId": "ocid1.compartment.oc1..unique_ID",
    "compartmentName": "example_compartment",
    "resourceName": "my_instance",
    "resourceId": "ocid1.instance.oc1.phx.unique_ID",
    "availabilityDomain": "availability_domain",
    "additionalDetails": {
      "imageId": "ocid1.image.oc1.phx.unique_ID",
      "shape": "VM.Standard2.1",
      "type": "CustomerVmi"
    }
  },
  "eventID": "unique_ID",
  "extensions": {
    "compartmentId": "ocid1.compartment.oc1..unique_ID"
  }
}
Enter fullscreen mode Exit fullscreen mode

This is an example of an instance that have been just created, and will trigger the event.


  • You can add conditions or filters to further narrow your events.

During an event rule configuration, you can add some attributes to further filter your results
Adding attribute to the rule

On this example we are filtering results from the event type, for changes only on Sandbox and dev compartments, the whole logic looks like:

MATCH event WHERE (
  eventType EQUALS ANY OF (
  com.oraclecloud.computeapi.launchinstance.end
  )
  AND (
  compartmentName MATCHES ANY OF (
  Sandbox,
  dev
  )
 )
)
Enter fullscreen mode Exit fullscreen mode

Additional to this, we can add Filter conditions based on Tags

MATCH event WHERE (
  eventType EQUALS ANY OF (
  com.oraclecloud.computeapi.launchinstance.end
  )
  AND (
  compartmentName MATCHES ANY OF (
  Sandbox,
  dev
  )
  definedTags INCLUDES ANY OF (
  Oracle-Tags.CreatedBy.elopez
  )
 )
)
Enter fullscreen mode Exit fullscreen mode

Visually something like:
Rule conditions configuration

Basically this event will trigger everytime an instance is launch on Sandbox OR dev compartments by the user elopez.


  • Rules most specify and action.

The main objective for the rules is to provoke something when they are triggered, otherwise will be useless.

Actions are responses defined for event matched

Actions can be created using:
Notifications send the messages to a notification service which can send it to endpoints that are subscribe to the topic
notifications
These could be: an email address, email group, slack channel, etc.

Streaming using this service you will ingest your events into a data streams to obtain further analysis and intelligence over this data.

Functions functions can be executed based on events received. Quick example:

you can have an event that triggers every time a new instance is created and this will execute a function that configure monitoring for that new instance.


  • You need to add permissions so the Events service can call the action services.

Take into consideration any action is deny by default on OCI so you will need to create a policy that permit the Events service to execute the actions.

Basic permission you will need is:

Allow service cloudEvents to use ons-topic in tenancy
Allow service cloudEvents to use functions-family in tenancy
Enter fullscreen mode Exit fullscreen mode

This is for the Tenancy but you can assign it to an specific Compartment

Allow service cloudEvents to use ons-topic in compartment DEV
Allow service cloudEvents to use functions-family in compartment DEV
Enter fullscreen mode Exit fullscreen mode

More about compartments HERE

💖 💪 🙅 🚩
aernesto24
Ernesto Lopez

Posted on January 11, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related