Skip to main content

drift

On a schedule, drift plans every provisioned instance that matches a set of attributes, reads each plan's logs for changes, and alerts Slack about drift. It can remove the drift by re-applying the configuration that is deployed. It writes each result to the stash.

How a check runs​

  1. The check lists the PROVISIONED instances whose effective attributes match match.

  2. For each instance, it reads the latest completed PROVISION deployment and calls planDeployment on it. The plan uses the params of that deployment, not the instance's saved params, so unsaved and undeployed edits do not show up as drift. It skips an instance with a deployment that is pending, approved, or running, and an instance with no completed PROVISION deployment.

  3. Massdriver sends deployment.completed, deployment.failed, or deployment.aborted for the plan. drift reads the plan's logs and gives the plan one result:

    ResultWhen
    driftedA drift pattern match counts one or more changes.
    cleanEvery step has a clean match, or a drift match with zero changes.
    unknownFewer matches than the bundle has steps, for example a Helm step.
    failedThe plan failed or was aborted, or no event came within 6 hours.
  4. It posts drifted, unknown, and failed results to Slack. The message has the change counts of each step, up to 20 lines of changed resources, and a link to the plan. Clean results are posted only with notify_clean: true.

  5. For a drifted result, mode decides what happens next. drift never resolves an unknown or failed result.

planDeployment plans the deployed params with the instance's current bundle version and dependencies. When the instance's resolved bundle version differs from the deployed version, or a dependency changed since the last provision, the plan also shows those changes. The alert names them, and drift does not resolve that instance.

Resolve modes​

moderesolve_with: deployed (default)resolve_with: saved
detect (default)Alert only.Alert only.
resolverollbackDeployment on the deployment that was planned, then approveDeployment on the proposal.createDeployment PROVISION with the instance's saved params.
proposerollbackDeployment on the deployment that was planned. A person approves it.proposeDeployment PROVISION with the instance's saved params. A person approves it.

deployed re-applies only what was already deployed: the params, bundle version, and dependency snapshot of the last completed PROVISION. The saved params can hold edits that nobody deployed yet, and saved deploys those edits along with the drift fix.

rollbackDeployment pins the instance to the bundle version of the deployment it re-applies. Massdriver sets the message of a rollback to Rollback to deployment <id> (v<version>). A saved deployment has the message drift: <check> resolving drift found by plan <plan id>.

When the environment has separation of duty, the service account cannot approve its own rollback. The proposal stays open and the alert says to approve it in Massdriver.

With resolve or propose, a reply in the alert's thread says what drift did: the deployment it created, or why it did not create one. With only a webhook, the reply is a message of its own.

Results in the stash​

When a plan completes, drift writes two JSON entries:

KeyStashValue
md/v1/driftThe instance{"status": "detected", "resources": 2, "checked_at": "2026-11-27T20:03:24Z"}. status is none, detected, or unknown. resources is the sum of the counts in the plan summaries.
md/v1/changes/driftThe plan deployment{"tool": "drift", "add": 0, "change": 1, "destroy": 1}, summed over the bundle's steps. Not written when the result is unknown, because some steps have no count.

Entries in a deployment's stash are immutable: when Massdriver sends an event again, the first value stays. A failed write is logged at debug and never fails the event. The UI shows both keys as widgets. Refer to well-known keys. With results.prefix set outside md/, such as results/v1, each result shows as a labeled metadata row.

Settings​

The config file is YAML, at CONFIG_FILE (default /etc/massdriver-automations/config.yaml). An unknown key stops the automation at start. config.example.yaml has every key, with comments.

KeyDefault
slack.channelThe channel for alerts of checks that name no channel of their own. Without a channel or a webhook, alerts go to the logs only.
massdriver.app_urlhttps://app.massdriver.cloudThe Massdriver UI, for the plan links.
results.prefixmd/v1The start of the stash result keys.
checks[].nameRequired. Unique. Alerts and POST /drift/run?check=<name> use it.
checks[].scheduleA cron expression (5 fields, or @daily, @hourly, @every 6h). Without one, the check runs only from POST /drift/run.
checks[].timezoneUTCThe IANA timezone of schedule.
checks[].matchEffective attributes, such as md-environment: prod. A list matches any of its values. Keys are combined with AND. Empty matches every provisioned instance.
checks[].modedetectdetect, resolve, or propose.
checks[].resolve_withdeployeddeployed or saved.
checks[].max_concurrent5Plans running at once for the check. The rest wait in a queue.
checks[].slack.channelslack.channelThe channel for this check's alerts.
checks[].notify_cleanfalsePost clean results.
checks[].summaryfalsePost one line of totals when a run finishes.
checks[].patterns.cleanNo changes\.A Go (RE2) regular expression that marks a step with no changes.
checks[].patterns.driftPlan: \d+ to \w+(?:, \d+ to \w+)*A Go regular expression that marks a step with changes. Each <n> to <verb> in the match is counted, so import and forget count too.
checks[].patterns.resource(?m)^\s*# \S+.* (?:will be\|must be) .*$A Go regular expression for the changed resource lines in the alert.
yaml
slack:
channel: "#infra-drift"
checks:
- name: prod-nightly
schedule: "0 6 * * *"
timezone: America/Chicago
match:
md-environment: prod
team: [payments, billing]
mode: propose
resolve_with: deployed
slack:
channel: "#payments-oncall"
summary: true
- name: sandbox-weekly
schedule: "0 9 * * MON"
match:
md-project: sandbox
mode: resolve
resolve_with: saved
max_concurrent: 2

The bundle's Settings params have the keys of the config file, with these differences:

  • A match value is one string. Separate values with commas, such as api, web, to match any of them. The bundle writes it as a list.
  • checks[].name must be lowercase letters, numbers, and hyphens.
  • checks[].match, mode, and resolve_with are required in the form, and the form starts with one check, prod-nightly, that alerts on md-environment = prod at 06:00 UTC.

Run a check now​

shell
curl -X POST -H "Authorization: Bearer $DRIFT_RUN_TOKEN" "https://drift.example.com/drift/run?check=prod-nightly"

The response is 202. A request for a check that does not exist gets 404.

Secrets​

Variable
SLACK_BOT_TOKENThe xoxb- token of the Slack app. Posts go to the channels in the settings.
SLACK_WEBHOOK_URLAn incoming webhook, for a workspace without the Slack app. Every post goes to its one channel. Ignored when the bot token is set.
DRIFT_RUN_TOKENTurns on POST /drift/run. Callers send it as Authorization: Bearer <token>.

drift also reads STATE_DIR, a directory for the plans in progress and the queues (drift.json). The bundle sets it to a volume. The MASSDRIVER_* variables are in the common settings.

Permissions​

ModePermissions on the matched instances
every modeproject:view on their projects, instance:plan, and stash:write
resolve, deployedinstance:propose and instance:deploy
resolve, savedinstance:deploy
proposeinstance:propose

Deployed with the bundle, the service account also needs organization:manageEventDestinations.

Host​

The bundle serves drift at Ingress > Host, such as drift.automations.acme.com. On ECS, an empty host uses drift.<domain of the cluster's hosted zone>. /hooks/massdriver must be public, and /drift/run too to start checks from outside the cluster.

State storage (GiB) sizes the Kubernetes volume for STATE_DIR. On ECS, the state is on EFS. Refer to Deploy.

Event destination​

The bundle creates a destination for deployment.completed, deployment.failed, and deployment.aborted, with the condition .data.metadata.deployment_action == "PLAN" and the attributes of Event attributes.

To create it yourself, with mass 2.6.0 or later:

shell
mass event destination create \
--name drift \
--url https://drift.example.com/hooks/massdriver \
--event-type deployment.completed \
--event-type deployment.failed \
--event-type deployment.aborted \
--condition '.data.metadata.deployment_action == "PLAN"'

The condition drops PROVISION and DECOMMISSION deployments, so Massdriver marks them SKIPPED and does not send them. drift ignores plans that it did not start.

Copy the signing secret from the destination's Overview tab into MASSDRIVER_SIGNING_SECRET.

Run​

shell
docker run -p 8080:8080 \
-v "$PWD/config.yaml:/etc/massdriver-automations/config.yaml:ro" \
-v "$PWD/state:/state" \
-e MASSDRIVER_ORGANIZATION_ID=acme \
-e MASSDRIVER_API_KEY=md_... \
-e MASSDRIVER_SIGNING_SECRET=whsec_... \
-e SLACK_BOT_TOKEN=xoxb-... \
-e DRIFT_RUN_TOKEN=... \
-e STATE_DIR=/state \
ghcr.io/massdriver-cloud/massdriver-automations:0.3 run drift

The image runs as user 65532. That user must be able to write to STATE_DIR.

Limits​

  • The API has no structured plan summary, so the result comes from log patterns. Output-only changes and provisioners other than OpenTofu and Terraform give unknown unless you set patterns.
  • The logs do not name their step. Matches are given step names in log order.
  • Run one replica. The schedule and the plans in progress live in one process.
  • Without STATE_DIR, a restart forgets the plans in progress and the queues. Their results are not read.
  • A plan that finishes before drift records its id is not read.