Webhook icon
Schedule icon
WorkingDirectory icon
Clone icon
Commands icon
Docker icon
Switch icon
SlackIncomingWebhook icon
Fail icon
Log icon

Terramate Multi-Stack Infrastructure Drift and Change Detection Gate

Audit Terramate stacks in Kestra to detect unmanaged infrastructure drift, resolve stack dependency ordering, and gate pull request changes before apply.

Categories
Infrastructure

Prevents out-of-order infrastructure provisioning, identifies circular dependency deadlocks, and detects unmanaged cloud configuration drift across multi-stack Terraform and OpenTofu codebases by orchestrating Terramate change detection and topological execution gates.

How it works

  1. Trigger Ingestion (infrastructure_pr_webhook, nightly_stack_drift_schedule): Initiates automatically when pull requests touch infrastructure code via authenticated webhook, or runs on a nightly schedule to audit configuration drift across all cloud stacks.
  2. Isolated Repository Checkout (clone_infrastructure_repo): Clones the infrastructure repository within an isolated io.kestra.plugin.core.flow.WorkingDirectory to ensure clean Git history for comparative change detection against the base branch.
  3. Terramate Multi-Stack Audit (audit_terramate_stacks): Executes a containerized Terramate and Terraform CLI inspection on Docker (io.kestra.plugin.scripts.shell.Commands) that discovers all stack.tm.hcl files under stacks_directory, tests HCL formatting, validates dependency ordering, isolates modified stacks via terramate list --changed --git-change-base, and, when audit_mode is DRIFT_AND_CHANGE_DETECTION, runs terraform plan -detailed-exitcode per stack to detect real out-of-band cloud drift.
  4. Governance Decision Routing (route_governance_decision): Evaluates findings via io.kestra.plugin.core.flow.Switch:
    • CIRCULAR_DEPENDENCY_ERROR: Alerts Slack (alert_cycle_error_slack) and halts pipeline execution (halt_on_dependency_cycle) when mutually recursive stack dependencies are detected.
    • UNEXPECTED_DRIFT_DETECTED: Alerts the platform team when terraform plan -detailed-exitcode reports real changes (exit code 2) on one or more stacks and halt_on_drift is enabled.
    • DRIFT_CHECK_INCONCLUSIVE: Alerts and halts when terraform plan itself failed on one or more stacks (typically missing backend credentials), since that is not the same as a verified-clean audit.
    • CHANGED_STACKS_REQUIRING_APPROVAL: Publishes the precise blast-radius inventory to Slack (notify_changed_stacks_slack) listing all modified stacks in topological execution sequence.
    • NO_STACKS_FOUND: Logs that no stacks matched stacks_directory; nothing was audited.
    • ALL_STACKS_SYNCHRONIZED (Default): Logs an audit certification receipt (log_clean_synchronization) confirming all stacks are synchronized and healthy.

What you get

  • Automated Blast-Radius Isolation: Pinpoints exactly which micro-stacks are modified by a pull request, avoiding hours of redundant plan executions across untouched environments.
  • Deadlock & Cycle Protection: Validates stack dependency graphs (before and after blocks) to prevent runtime deadlocks and state file corruption.
  • Continuous Cloud Drift Auditing: Nightly scans detect out-of-band cloud console modifications before they cause deployment conflicts.
  • Audit-Ready Topological Reporting: Maintains structured logs and Slack summaries detailing stack execution order and changed resource counts.

Who it's for

  • Platform Engineers & Cloud Architects managing modular, multi-tenant Terraform or OpenTofu estates across AWS, GCP, or Azure.
  • DevOps & SRE Teams implementing GitOps and continuous delivery pipelines for infrastructure with blast-radius guardrails.
  • Security & Compliance Officers requiring verification that infrastructure states strictly mirror version-controlled Git definitions.

Why orchestrate this with Kestra

  • Unified Control Plane: Coordinates Git repository checkout, containerized Terramate CLI execution, dynamic branch routing, and incident alerting in a single declarative YAML workflow.
  • Event-Driven & Scheduled Governance: Seamlessly unifies webhook-driven pull request gating with automated recurring cron schedules for continuous drift monitoring.
  • Secret Masking & Security: Securely injects Git tokens and webhook credentials through {{ secret('NAME') }} without exposing credentials in task logs or state backends.

Prerequisites

  • A running Kestra v2.0 instance with Docker task runner support (plugin-scripts-runner-docker).
  • Network access to clone the infrastructure Git repository.

Secrets

  • INFRASTRUCTURE_WEBHOOK_KEY: Secret authentication key used to validate incoming GitHub/GitLab webhook payloads (required).
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook URL for alerting on drift breaches, cycles, and blast-radius summaries (optional).
  • GITHUB_TOKEN: Personal Access Token or GitHub App token for cloning private infrastructure repositories (optional).

Actual drift detection (audit_mode: DRIFT_AND_CHANGE_DETECTION) runs terraform plan per stack, which needs real backend and cloud provider credentials configured for that stack. Without them, terraform plan fails and the gate reports DRIFT_CHECK_INCONCLUSIVE rather than a false clean pass. Set audit_mode to CHANGE_DETECTION_ONLY or DEPENDENCY_DAG_VALIDATION_ONLY to skip the live plan step entirely and run with no cloud credentials at all.

Quick start

  1. Configure INFRASTRUCTURE_WEBHOOK_KEY in your Kestra namespace secrets.
  2. Import this blueprint into your Kestra instance under the company.infrastructure namespace.
  3. Set audit_mode to CHANGE_DETECTION_ONLY and click Execute with the default repository to see stack discovery, HCL formatting, and dependency ordering against the real official Terramate AWS quickstart repository, with no cloud credentials required.
  4. Set git_repository_url to your team's Terramate repository to audit production stacks.
  5. Configure infrastructure_pr_webhook in your Git provider's webhook settings to trigger on pull requests.

How to extend

  • Automated Plan Generation: Extend the audit script to run terramate run -- terraform plan -out=tfplan exclusively on changed stacks and upload plans as Kestra execution artifacts.
  • Pull Request Annotations: Add an io.kestra.plugin.github.issues.Create or comment task to post the generated stack impact Markdown table directly onto the GitHub pull request.
  • OpenTofu Integration: Switch the runtime engine to OpenTofu by setting TERRAMATE_TERRAFORM_BINARY=tofu to leverage open-source state backends.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.