Home / OCI

OCIHow-toIntermediate

OCI Resource Manager drift detection: find and fix resources changed outside Terraform

Run drift detection on a Resource Manager stack, read which resources were modified or deleted outside Terraform, and decide whether to revert, adopt or ignore each change.

Deepak Bhardwaj7 min read

Applies toOCI Resource Manager (Terraform 0.14+ stacks for provider upgrade), OCI CLI 3.x, Terraform provider for OCI

The goal

Someone opened a port in a security list from the Console during an incident. Someone else resized a boot volume by hand. Now the stack's next plan wants to "fix" things nobody expected, or worse, an apply silently undoes the incident fix.

Drift detection compares the real state of each resource in a stack with what the Terraform state says it should be. Run it before every planned apply and on a schedule, so you find out-of-band changes on your terms.

Set variables first:

export STACK=<stack-ocid>
export C=<compartment-ocid>

Tip

Drift detection only works on stacks whose last job was Apply or Import state, or on stacks created from a compartment. If the last job was a failed apply or a destroy, fix that first. See the Resource Manager runbook for stuck or failed jobs.

1. Run drift detection

Check the whole stack and wait for the work request to finish:

oci resource-manager stack detect-drift \
  --stack-id $STACK \
  --wait-for-state SUCCEEDED --wait-for-state FAILED \
  --max-wait-seconds 1800

The command starts a work request. Large stacks take a while because Resource Manager reads every resource back from the OCI APIs. The default wait is 1,200 seconds; raise --max-wait-seconds for big stacks. Exit code 2 means the wait timed out, not that detection failed.

Check only some resources

If you only care about the network layer, pass resource addresses as JSON. Addresses use the Terraform form type.name[index]:

oci resource-manager stack detect-drift \
  --stack-id $STACK \
  --resource-addresses '["oci_core_security_list.app","oci_core_instance.web[0]"]' \
  --wait-for-state SUCCEEDED --wait-for-state FAILED

If the shape of the JSON is rejected by your CLI version, generate a template with --generate-param-json-input resource-addresses and fill it in.

Provider upgrade during detection

--is-provider-upgrade-required true upgrades the OCI provider within the stack's version constraints before checking. Only use it if the stack is on Terraform 0.14 or later and you are ready for the next apply to use the same newer provider.

2. Read the results

List only the resources that changed:

oci resource-manager stack list-resource-drift-details \
  --stack-id $STACK \
  --resource-drift-status MODIFIED --all

And the ones that no longer exist:

oci resource-manager stack list-resource-drift-details \
  --stack-id $STACK \
  --resource-drift-status DELETED --all

Without --work-request-id, results come from the latest completed detection on the stack. To compare with an older run, pass that work request's OCID.

The status values are:

Status Meaning
IN_SYNC Real resource matches the state
MODIFIED One or more properties differ from the state
DELETED Resource is in the state but no longer exists in OCI
NOT_CHECKED Resource was not checked (outside the selected addresses, or not evaluated)

Each item lists the resource address, its OCID and the actual versus expected properties. A compact view with jq:

oci resource-manager stack list-resource-drift-details \
  --stack-id $STACK --resource-drift-status MODIFIED --all \
  | jq -r '.data.items[] | [."resource-address", ."resource-drift-status", ."resource-id"] | @tsv'

Field names in the JSON vary slightly between CLI versions. Run one call without jq first and check the keys.

3. Find out who changed it

Drift tells you what changed, not who. Take the resource OCID from the drift output and search Audit for the last week:

oci logging-search search-logs \
  --search-query "search \"$C/_Audit\" | where data.resourceId = '<resource-ocid>' | sort by datetime desc" \
  --time-start $(date -u -d '-7 days' +%Y-%m-%dT%H:%M:%SZ) \
  --time-end $(date -u +%Y-%m-%dT%H:%M:%SZ)

Look at data.eventName and data.identity.principalName. A change at 03:00 by an on-call engineer during an incident is very different from one made by an unknown automation user.

4. Decide what to do with each change

Drift Typical cause What to do
Security list or NSG rule added Incident fix in the Console If the rule is needed, add it to the Terraform code, then apply (no-op). If not, apply to revert.
Instance shape or OCPUs changed Manual resize Update the variable in code to match, or apply to roll back in a maintenance window.
Boot or block volume size larger Online expansion Update code to the new size. Never apply a smaller size; volumes can't shrink.
Tags differ Tag defaults or Cloud Guard/automation adding tags Add lifecycle { ignore_changes = [defined_tags] } if another system owns the tags.
Resource DELETED Deleted by hand or by another stack Decide whether it should exist. Apply recreates it; or remove it from code and state.
Same field drifts every run Provider computes or normalises a value Treat as noise; add that attribute to ignore_changes.

Warning

Running an apply on a drifted stack reverts every modified resource to the code. If someone opened a port to stop an outage, an apply closes it again. Read the plan job's log before you approve any apply after drift is found.

Adopt the change into code

  1. Edit the Terraform configuration so it matches the actual properties from the drift output.
  2. Upload the new configuration to the stack (or push to the source repository the stack uses).
  3. Run a plan job and confirm it shows no changes for that resource:
oci resource-manager job create-plan-job --stack-id $STACK \
  --wait-for-state SUCCEEDED --wait-for-state FAILED

oci resource-manager job get-job-logs-content --job-id <plan-job-ocid> \
  | jq -r '.data' | grep -E 'Plan:|No changes'
  1. Run the apply, then run drift detection again and confirm IN_SYNC.

Revert the change

Run a plan job, check that the only changes are the drifted ones, then apply that plan:

oci resource-manager job create-apply-job --stack-id $STACK \
  --execution-plan-strategy FROM_PLAN_JOB_ID \
  --execution-plan-job-id <plan-job-ocid> \
  --wait-for-state SUCCEEDED --wait-for-state FAILED

Applying from a reviewed plan job means nothing extra sneaks in between review and apply.

5. Check drift across every stack in a compartment

A short loop runs detection on each active stack and prints how many resources are not in sync:

for s in $(oci resource-manager stack list --compartment-id $C \
            --lifecycle-state ACTIVE --all --query 'data[].id' --raw-output | jq -r '.[]'); do
  oci resource-manager stack detect-drift --stack-id $s \
    --wait-for-state SUCCEEDED --wait-for-state FAILED >/dev/null 2>&1
  n=$(oci resource-manager stack list-resource-drift-details --stack-id $s --all \
      | jq '[.data.items[] | select(."resource-drift-status"=="MODIFIED" or ."resource-drift-status"=="DELETED")] | length')
  echo "$s drifted=$n"
done

Detection jobs on different stacks can run in parallel, but each stack runs one job at a time. Don't run this loop while someone is applying.

Prevent it next time

  • Run drift detection before every apply and weekly from a scheduled job (a Function or a CI pipeline), and alert when anything is MODIFIED or DELETED.
  • Limit Console write access to resources owned by stacks. Give operators read or use in those compartments and keep manage for the Resource Manager automation group and break-glass accounts.
  • Agree an incident rule: any Console fix gets a ticket to add it to code within one working day.
  • Put attributes that another system legitimately owns (tags, autoscaled instance counts) into ignore_changes, so real drift isn't buried in noise.
  • Keep the stack's last job a successful apply. A failed apply left alone blocks drift detection and hides the next change.