Webhook icon
Schedule icon
WorkingDirectory icon
Commands icon
Docker icon
Script icon
If icon
SlackIncomingWebhook icon
Fail icon
Log icon

Triage Terraform Plan Drift Before the Next Deploy

Parse terraform show -json plan output for drifted resource attributes, alert Slack with the changed paths, and gate the next deploy in Kestra.

Categories
Infrastructure

How it works

  1. on_plan_ready (io.kestra.plugin.core.trigger.Webhook, keyed with the TERRAFORM_PLAN_WEBHOOK_KEY secret) receives the terraform show -json document from your drift detection pipeline right after each scheduled plan. daily_drift_sweep (io.kestra.plugin.core.trigger.Schedule, cron 0 5 * * *) is the safety net and ships disabled: true until an operator turns it on.
  2. plan_workspace (io.kestra.plugin.core.flow.WorkingDirectory) opens one shared directory so the staged plan and the parser see the same file.
  3. stage_plan_output (io.kestra.plugin.scripts.shell.Commands on the io.kestra.plugin.scripts.runner.docker.Docker runner, image alpine:3.20) writes tfplan.json with a heredoc. The fixture holds two managed resources, an aws_s3_bucket.audit_logs whose acl drifted from private to public-read under an update action, and an aws_security_group.edge sitting at no-op. When plan_path differs from the fixture name the file is copied there too.
  4. parse_plan (io.kestra.plugin.scripts.python.Script on python:3.12-slim) reads the plan at plan_path, keeps resource changes whose actions intersect update or replace, and recursively diffs the before and after objects down to the leaf attributes that actually moved. The verdict, the drifted resources and the flattened changed paths are printed as a single ::{"outputs": ...}:: line.
  5. drift_gate (io.kestra.plugin.core.flow.If) branches on plan_ok and has_drift. Drift reaches alert_drift (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) with the workspace and the changed paths, then enforce_gate (io.kestra.plugin.core.flow.If) applies the fail_on_drift policy with fail_execution (io.kestra.plugin.core.execution.Fail) or the report only log_gate_softened. A clean or unparseable plan lands on log_no_drift.
  6. The flow level errors handler (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) reports any failure with the execution id, because a triage that dies silently is indistinguishable from a clean plan.

What you get

  • Drift triage that names the exact attribute paths that moved, not just a count of changed resources.
  • A gating switch, fail the execution when drift exists or stay report only, controlled by one input.
  • Slack context carrying the workspace and the flattened paths so the owning team can reconcile immediately.
  • has_drift, drifted_count, drifted_resources and changed_attributes as typed outputs for ticket automation and compliance evidence.
  • A disabled daily sweep as a second net once the webhook path is trusted.

Who it's for

  • Platform teams running scheduled drift detection plans who need someone to triage newly detected changes before the next deploy.
  • DevOps engineers who want drift evidence attached to executions instead of buried in CI logs.
  • Compliance and security reviewers who need a dated record of out of band infrastructure changes.

Why orchestrate this with Kestra

terraform plan -detailed-exitcode tells you something changed but not what, and CI logs bury the resource list where nobody triages it. Kestra gives the plan a webhook to land on, a shared workspace for the fixture and the parser, a branch that separates drift from quiet, a policy input for fail versus report, and outputs a ticket or dashboard can consume. The same version controlled file carries the burn free drift rules, the escalation policy and the execution history that proves triage happened.

Prerequisites

  • A worker with Docker access, the fixture runs in alpine:3.20 and the parser in python:3.12-slim.
  • A drift detection pipeline that produces terraform show -json output and can POST it to the webhook, or rely on the bundled fixture for trial runs.
  • A Slack incoming webhook for the drift alert and for failure alerts.

Secrets

  • TERRAFORM_PLAN_WEBHOOK_KEY: key segment guarding the plan webhook URL, anyone holding it can trigger a triage run.
  • SLACK_WEBHOOK_URL: Slack incoming webhook for the drift alert and failure alerts.

Quick start

  1. Add the two secrets to your Kestra namespace.
  2. Run the flow once with the default inputs and confirm has_drift reads true, drifted_count reads 1, changed_attributes contains aws_s3_bucket.audit_logs.acl, and the Slack alert arrived.
  3. Re-run with fail_on_drift set to false and watch log_gate_softened keep the execution green while the alert still fires.
  4. Point your drift pipeline at /api/v1/{tenant}/executions/webhook/{namespace}/terraform-plan-drift-detect/{TERRAFORM_PLAN_WEBHOOK_KEY}, and enable daily_drift_sweep once the path is trusted.

How to extend

  • Replace the staged fixture with an io.kestra.plugin.core.http.Request that pulls the latest plan from your CI artifact store before parsing, keeping the fixture as the offline fallback.
  • Classify severity per resource type by extending the parser, for example treating security group and IAM drift as always fail regardless of the input flag.
  • Store each drifted plan in object storage with io.kestra.plugin.aws.s3.Upload to build a compliance evidence trail.
  • Fan out multi workspace plans with io.kestra.plugin.core.flow.ForEachItem so each workspace gets its own triage execution.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.