Schedule icon
Commands icon
Docker icon
If icon
SlackIncomingWebhook icon
Log icon

Monitor Terraform State Locks

Detect stuck Terraform state locks and alert Slack so they get unlocked deliberately, not by accident.

Categories
CloudInfrastructureinfrastructure

A wedged state lock blocks every plan and apply until someone remembers force-unlock. This blueprint polls the lock age every 30 minutes and alerts Slack when it outlives your threshold - with the exact unlock command in the message.

How it works

  1. check_lock (io.kestra.plugin.scripts.shell.Commands on the Terraform image) heads the .tflock object in the state bucket and emits its age via the ::{"outputs": ...}:: protocol.
  2. stale_lock (io.kestra.plugin.core.flow.If) branches: stale -> alert_stale_lock; fresh -> log_ok.
  3. The errors block alerts on failure.
  4. Trigger: a disabled */30 Schedule.

What you get

  • Early, deliberate unlock instead of a blocked pipeline and a blind force-unlock.
  • The lock age in the execution history.

Who it's for

  • Teams with shared state and multiple humans running applies.
  • Anyone who has lost an hour to a dead apply holding the lock.

Why orchestrate this with Kestra

AWS tells you when the lock object was written; the flow turns that into a decision with a runbook. The next step (auto-unlock after approval, open a ticket, page the owner) is one task away.

Prerequisites

  • Docker available on the Kestra Worker.
  • Read access to the state bucket.
  • A Slack webhook.

Secrets

  • SLACK_WEBHOOK_URL: webhook for stale-lock and failure alerts.
  • TF_STATE_BUCKET: S3 bucket holding the state.
  • TF_STATE_KEY: state object key prefix.

Quick start

  1. Add the Slack webhook and state secrets.
  2. Set lock_id and max_age_minutes.
  3. Run once and read lock_age_minutes.
  4. Enable the schedule.

How to extend

  • Gate the unlock behind a Slack approval task.
  • Alert only when no apply is running (check CI status).
  • Track lock history in KV.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.