OCI Anomaly Detection: Unsupervised Time Series Anomaly Detection

OCI Anomaly Detection trains unsupervised machine learning models on historical time series data and scores new data points in real time to identify anomalies. Unlike threshold alarms that fire when a metric crosses a fixed value, anomaly detection learns normal patterns including seasonality, trends, and correlations between signals, then flags deviations from those learned patterns. No labeled anomaly examples are needed for training.

Step 1: IAM Policy and Project

resource "oci_identity_policy" "anomaly_detection_policy" {
  compartment_id = var.compartment_id
  name           = "anomaly-detection-policy"
  statements = [
    "Allow group AIEngineers to manage ai-service-anomaly-detection-family in compartment id COMPARTMENT_OCID",
    "Allow service ai-service-anomaly-detection to read object-family in compartment id COMPARTMENT_OCID"
  ]
}

resource "oci_ai_anomaly_detection_project" "infra_anomaly" {
  compartment_id = var.compartment_id
  display_name   = "infrastructure-anomaly-detection"
  description    = "Anomaly detection for OCI infrastructure metrics"
  defined_tags   = { "Operations.ManagedBy" = "terraform" }
}

Step 2: Training Data Asset and Model

resource "oci_ai_anomaly_detection_data_asset" "metrics_dataset" {
  compartment_id = var.compartment_id
  project_id     = oci_ai_anomaly_detection_project.infra_anomaly.id
  display_name   = "infrastructure-metrics-90d"
  description    = "90 days of CPU, memory, latency, and error rate metrics for training"

  data_source_details {
    data_source_type = "ORACLE_OBJECT_STORAGE"
    namespace        = var.tenancy_namespace
    bucket_name      = var.training_bucket_name
    object_name      = "training/infrastructure-metrics-90d.csv"
  }
}

resource "oci_ai_anomaly_detection_model" "infra_model" {
  compartment_id = var.compartment_id
  project_id     = oci_ai_anomaly_detection_project.infra_anomaly.id
  display_name   = "infrastructure-anomaly-model-v1"

  model_training_details {
    target_fap        = 0.01  # Target 1% false alarm probability
    training_fraction = 0.7
    data_asset_ids    = [oci_ai_anomaly_detection_data_asset.metrics_dataset.id]
    algorithm_hint    = "MULTIVARIATE_MSET"  # Multi-variate: learns correlations between signals
  }

  defined_tags = { "Operations.ManagedBy" = "terraform" }
}

output "model_id" { value = oci_ai_anomaly_detection_model.infra_model.id }

Step 3: Anomaly Detection Alarm

resource "oci_monitoring_alarm" "anomaly_high_score" {
  compartment_id        = var.compartment_id
  display_name          = "infrastructure-anomaly-detected"
  is_enabled            = true
  metric_compartment_id = var.compartment_id
  namespace             = "oci_ai_anomaly_detection"
  query                 = "AnomalyScore[5m]{modelId = 'MODEL_OCID'}.max() > 0.8"
  severity              = "WARNING"
  pending_duration      = "PT5M"
  destinations          = [var.ops_notification_topic_id]
  body                  = "Infrastructure anomaly score above 0.8. Review the anomaly detection console for which signals deviated and when."
}

Operational Notes

Training data quality determines model quality. Provide at least 30 days of historical data, ideally 90 days, to capture weekly and monthly seasonality. Data with known outage windows included in the training set degrades model quality because the model learns the anomalous pattern as normal. Exclude known outage time ranges from your training CSV before uploading.

The target_fap parameter controls the false alarm probability. Setting it to 0.01 means the model aims to flag 1 percent of normal data points as anomalies. Lower values reduce false alarms but may miss real anomalies. Start at 0.01 and adjust based on how many false positives your team can handle per week before alert fatigue affects response quality.

Regards,
Osama

#OCI #OracleCloud #AnomalyDetection #AI #TimeSeries #Terraform #IaC #TechBlog #Oracle #MachineLearning #MLOps #Observability #OracleCloudInfrastructure #UnsupervisedML #AIOps #Monitoring #InfrastructureIntelligence #DataScience #CloudAI #PredictiveAnalytics

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.