NewCompare CPU & GPU pricing across AWS, Azure & GCP
Terraform

Workload Rule

Manage per-workload vertical and horizontal scaling rules with Terraform.

Workload Rule

devzero_workload_rule configures vertical and horizontal scaling for a specific Kubernetes workload — CPU, memory, GPU, and HPA rules — along with emergency response and per-container overrides.

Example Usage

Auto-generate all fields

resource "devzero_workload_rule" "auto" {
  cluster_id    = "<YOUR_CLUSTER_ID>"
  namespace     = "production"
  kind          = "Deployment"
  name          = "my-api"
  auto_generate = true
}

Manual — full control

resource "devzero_workload_rule" "manual" {
  cluster_id = "<YOUR_CLUSTER_ID>"
  namespace  = "production"
  kind       = "Deployment"
  name       = "my-api"

  action_triggers    = ["on_schedule", "on_detection"]
  cron_schedule      = "0 2 * * *"
  detection_triggers = ["pod_creation", "pod_update", "pod_evict"]

  cpu_rule = {
    enabled                   = true
    min_request               = 10
    max_request               = 32000
    target_percentile         = 0.95
    limits_adjustment_enabled = true
    limit_multiplier          = 1.0
  }

  memory_rule = {
    enabled                   = true
    min_request               = 67108864    # 64Mi in bytes
    max_request               = 68719476736 # 64Gi in bytes
    target_percentile         = 0.95
    limits_adjustment_enabled = true
  }

  hpa_rule = {
    enabled            = true
    min_replicas       = 1
    max_replicas       = 10
    target_utilization = 0.70
    primary_metric     = "cpu"
  }

  emergency_response = {
    oom_enabled               = true
    oom_memory_multiplier     = 1.5
    cpu_throttling_enabled    = true
    cpu_throttling_threshold  = 0.20
    cpu_throttling_multiplier = 1.25
  }

  live_migration_enabled        = false
  use_in_place_vertical_scaling = false
}

Per-container rules

resource "devzero_workload_rule" "per_container" {
  cluster_id = "<YOUR_CLUSTER_ID>"
  namespace  = "production"
  kind       = "Deployment"
  name       = "my-multi-container-app"

  action_triggers    = ["on_detection"]
  detection_triggers = ["pod_creation"]

  containers = [
    {
      container_name = "app"
      cpu_rule = {
        enabled     = true
        min_request = 10
        max_request = 32000
      }
      memory_rule = {
        enabled     = true
        min_request = 67108864    # 64Mi
        max_request = 68719476736 # 64Gi
      }
    },
    {
      container_name = "sidecar"
      cpu_rule = {
        enabled     = true
        max_request = 500
      }
    }
  ]
}

Arguments

Required

ParameterTypeDescription
cluster_idstringID of the cluster this rule targets
kindstringKubernetes workload kind
namestringName of the Kubernetes workload
namespacestringKubernetes namespace of the workload

Optional

ParameterTypeDescription
action_triggerslist(string)When to apply recommendations: "on_detection", "on_schedule"
auto_generateboolWhen true, the engine generates all rule fields automatically; manual overrides are ignored
containerslist(object)Per-container rule configurations (see Containers)
cpu_ruleobjectCPU vertical scaling rule (see Resource Rule)
cron_schedulestring5-field UTC cron expression for scheduled application
defragmentation_schedulestringCron expression for node defragmentation
detection_triggerslist(string)Events that trigger a recommendation: "pod_creation", "pod_update", "pod_evict"
emergency_responseobjectEmergency response for OOM and CPU throttle events (see Emergency Response)
gpu_ruleobjectGPU vertical scaling rule (see Resource Rule)
hpa_ruleobjectHorizontal scaling rule (see HPA Rule)
live_migration_enabledboolAllow live pod migration when applying recommendations
memory_ruleobjectMemory vertical scaling rule (see Resource Rule)
scheduler_pluginslist(string)Kubernetes scheduler plugins to activate
use_in_place_vertical_scalingboolUse in-place pod vertical scaling instead of pod restarts

Read-Only

AttributeTypeDescription
idstringUnique identifier of the workload rule

Resource Rule

Used by cpu_rule, memory_rule, and gpu_rule at both workload and per-container level.

ParameterTypeDescription
enabledboolEnable this resource axis rule
min_requestnumberMinimum resource request (millicores for CPU, bytes for memory/GPU)
max_requestnumberMaximum resource request (millicores for CPU, bytes for memory/GPU)
target_percentilenumberPercentile of usage data used as the recommendation target (0–1)
limits_adjustment_enabledboolWhether to also adjust resource limits alongside requests
limits_removal_enabledboolActively remove limits from workloads
limit_multipliernumberMultiplier applied to the request to derive the resource limit
max_scale_up_percentnumberMaximum percentage increase allowed in a single cycle
max_scale_down_percentnumberMaximum percentage decrease allowed in a single cycle

max_scale_up_percent and max_scale_down_percent are available on workload-level cpu_rule, memory_rule, and gpu_rule but not on per-container rules.

HPA Rule

ParameterTypeDescription
enabledboolEnable horizontal (replica) scaling
min_replicasnumberMinimum number of replicas
max_replicasnumberMaximum number of replicas
primary_metricstringPrimary metric: "cpu", "memory", "gpu", "network_ingress", "network_egress"
target_utilizationnumberTarget CPU utilization ratio (0–1)
target_memory_utilizationnumberTarget memory utilization ratio (0–1), tuned independently of CPU
max_replica_change_percentnumberMaximum percentage change in replica count per cycle
composite_formulastringFormula combining multiple metric weights. Example: "0.6*cpu + 0.4*memory"
metricslist(object)Additional metric triggers (see HPA Metrics)
behaviorobjectFine-grained scale-up/scale-down policies (see HPA Behavior)
fallbackobjectReplica fallback when metrics are unavailable (see HPA Fallback)

HPA Metrics

CPU/Memory/Network triggers are auto-generated from primary_metric. Use metrics for additional sources such as Prometheus.

ParameterTypeDescription
typestringMetric source type. Example: "CPU", "Memory", "prometheus"
target_utilizationstringTarget utilization as a decimal string. Example: "0.70"
target_valuestringAbsolute target value as a string. Example: "50000000"
weightstringWeight for composite formula scaling (0–1 decimal string). Example: "0.5"
server_addressstringPrometheus server URL. Example: "http://prometheus:9090"
querystringPromQL query string. Example: "sum(rate(http_requests_total[2m]))"
metadatamap(string)Free-form key-value metadata for external scalers

HPA Behavior

ParameterTypeDescription
scale_upobjectScale-up behavior (see Scaling Policy)
scale_downobjectScale-down behavior (see Scaling Policy)

Scaling Policy

ParameterTypeDescription
stabilization_window_secondsnumberSeconds to wait before acting on a scaling signal to avoid flapping
select_policystringWhich policy wins when multiple match: "Max", "Min", "Disabled"
policieslist(object)List of step policies

Each policy object requires:

ParameterTypeDescription
typestringPolicy type: "Pods" or "Percent"
valuenumberPolicy value (pod count or percent)
period_secondsnumberPeriod over which the policy applies

HPA Fallback

ParameterTypeRequiredDescription
replicasnumberYesNumber of replicas to fall back to when metrics are unavailable
behaviorstringNoFallback strategy: "static", "currentReplicas", "currentReplicasIfHigher", "currentReplicasIfLower"
failure_thresholdnumberNoConsecutive metric failures before activating fallback

Emergency Response

ParameterTypeDescription
oom_enabledboolReact to OOM kills by increasing memory
oom_memory_multipliernumberMultiplier applied to memory on OOM reaction
cpu_throttling_enabledboolReact to CPU throttling by increasing CPU request
cpu_throttling_thresholdnumberThrottle ratio threshold that triggers a reaction (0–1)
cpu_throttling_multipliernumberMultiplier applied to CPU request on throttle reaction

Containers

When containers is set, the workload-level cpu_rule, memory_rule, and gpu_rule are ignored. Each container entry accepts:

ParameterTypeRequiredDescription
container_namestringYesName of the container this config applies to
cpu_ruleobjectNoCPU resource rule (see Resource Rule)
memory_ruleobjectNoMemory resource rule (see Resource Rule)
gpu_ruleobjectNoGPU resource rule (see Resource Rule)

On this page