Working with events in Oracle Cloud Infrastructure Part 1: service basics
Tags: beginners, cloud, eventdriven, oracle
Published: 2022-01-11 22:55:21

**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](https://docs.oracle.com/en-us/iaas/Content/Functions/Concepts/functionsoverview.htm)


###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
  )
)
```
This event will trigger when a new [compute instance](https://docs.oracle.com/en-us/iaas/Content/Compute/Concepts/computeoverview.htm#:~:text=An%20Oracle%20Cloud%20Infrastructure%20VM,software%20stack%2C%20and%20networking%20infrastructure.) 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"
  }
}
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpnrovo3flyxnhuocb40.png)
 
**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
  )
 )
)
```

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
  )
 )
)
```
Visually something like:
![Rule conditions configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/37afhkz6yk5g4kykfvr3.png)

**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](https://docs.oracle.com/en-us/iaas/Content/Notification/home.htm#top)** send the messages to a notification service which can send it to endpoints that are subscribe to the topic
![notifications](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhfx6lgpbd97ekk5gx90.png)
 These could be: an email address, email group, slack channel, etc.

**[Streaming](https://docs.oracle.com/en-us/iaas/Content/Streaming/home.htm#top)** using this service you will ingest your events into a data streams to obtain further analysis and intelligence over this data.

**[Functions](https://docs.oracle.com/en-us/iaas/Content/Functions/Concepts/functionsoverview.htm#Overview_of_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
```

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

More about compartments [HERE](https://dev.to/aernesto24/7-things-to-know-about-compartments-on-oracle-cloud-infrastructure-4l1p)