Oracle Cloud Observability Terraform module

Karthic
3 min readSep 17, 2023

--

There is a new OCI(Oracle Cloud Infrastructure) terraform module published for Observability

OCI Observability terraform module

As of this writing there are three submodules available to create alarms, service connector and log analytics sources. In this blog I will show you how to create multiple Alarms using this module .

If you are new to OCI Alarms please check the doc here. We setup alarm to alert us via email or other notification when a monitoring metric is breached or an event happened. To monitor a cloud environment we would need many such alarms and it will be cumbersome to create many alarms using the console.

Let’s utilise this terraform module to create many alarms defined in csv file.Lets create a main.tf file with the below code in OCI Cloudshell. If you are running locally please set the required provider authentication.

provider oci {}

terraform {
required_providers {
oci = {
source = "oracle/oci"
version = ">= 5.0.0"
}
}
required_version = ">= 1.0.0, <= 1.5.6"
}

variable "compartment_ocid" {
type = string
description = "Compartment OCID"
}

module "observability_alarm" {
source = "oracle-terraform-modules/observability/oci//modules/alarm"
version = "0.0.2"
# insert the 3 required variables here
alarm_def = local.alarm_def
compartment_ocid = var.compartment_ocid
notification = local.notification
}

locals {
alarm_csv = csvdecode(file("${path.root}/alarm.csv"))
alarm_def = { for alarm in local.alarm_csv : alarm.key => alarm }
topicmergelist = distinct([for alarm in local.alarm_def : { create_topic = alarm.create_topic, destination = alarm.destination }])
notification = { for topic in local.topicmergelist : topic.destination => { create_topic = topic.create_topic } }
}

Create a alarm.csv file in the below format.To create alarm we need display_name, namespace of the metric it belongs in this example its oci_computeagent. Then the MQL query to create alarm like CPUUtilization mean statistic greater than 80 or MemoryUtilization mean statistic greater than 80.

The destination topic name is required.In this example a topic named email already exists so the create_topic is set to false. If you want the topic to be created you can set the create_topic to true and the severity of the alarm. The key can be any name you want that should not change in each run. If you change that it will recreate the resource.

key,display_name,namespace,query,destination,severity,create_topic
alarm1,HighCPUalarm,oci_computeagent,CpuUtilization[1m].mean() > 80,email,WARNING,false
alarm2,HighMemalarm,oci_computeagent,MemoryUtilization[1m].mean() > 80,email,INFO,false

Run the terraform command as below to execute

terraform init (To initialize the provider and download the module)

terraform plan (To check the code plan on what it would create )

terraform apply -auto-approve (To apply the code to create the infrastructure)

You can create n number of alarms easily with this method .

OCI Alarm

By default the trigger delay is set to 5 minutes and body message will be empty.Let’s customise further by adding the trigger and body in the csv file.

key,display_name,namespace,query,destination,severity,create_topic,trigger,body
alarm1,HighCPUalarm,oci_computeagent,CpuUtilization[1m].mean() > 80,email,WARNING,false,PT1M,"Run top command"
alarm2,HighMemalarm,oci_computeagent,MemoryUtilization[1m].mean() > 80,email,INFO,false,PT2M,"Run free -m command"
Alarm body message
Trigger Rule set to 1 minute

--

--

No responses yet