Request icon
Script icon
Docker icon
Switch icon
If icon
Loop icon
Log icon
SlackIncomingWebhook icon
Fail icon

Atlantis Terraform Workspace Lock Reconciler and Deadlock Reclaim Guard

Reconcile Atlantis Terraform locks against GitHub PR states, automatically unlock orphaned workspaces from closed PRs, and prevent CI deadlocks with Kestra.

Categories
Infrastructure

Prevent Terraform GitOps pipeline deadlocks by reconciling Atlantis project locks against live GitHub pull request states. In infrastructure engineering organizations using Atlantis to automate Terraform workflows through pull request comments (atlantis plan, atlantis apply), Atlantis acquires exclusive workspace locks whenever plans are generated.

A frequent and disruptive operational failure occurs when pull requests are closed without merging, abandoned, or merged outside of the standard Atlantis comment flow. When this happens, Atlantis retains the project workspace lock indefinitely. Subsequent pull requests touching the same directory or environment are immediately rejected with Project is locked by PR #<number>, blocking engineering releases until an administrator manually logs into the Atlantis console to find and delete the stranded lock.

This blueprint implements an automated reconciliation engine that polls the Atlantis /locks API, interrogates GitHub pull request states for each lock holder, detects orphaned locks (associated with closed, merged, or deleted pull requests), safely releases them via the Atlantis HTTP DELETE API, and alerts engineering teams on Slack when stale locks threaten release cadence.

How it works

  1. fetch_atlantis_locks (io.kestra.plugin.core.http.Request) calls the Atlantis GET /api/locks endpoint with HTTP basic auth to list every held Terraform project and workspace lock.
  2. reconcile_locks_with_github (io.kestra.plugin.scripts.python.Script) executes a Python script in a containerized runner that:
    • Confirms the GitHub token can read github_repository, then keeps only the locks whose ProjectRepo is that repository, because one Atlantis server can hold locks for many repositories.
    • Interrogates the GitHub REST API (GET /repos/{owner}/{repo}/pulls/{PullID}) to determine the real-time PR state and last activity timestamp.
    • Categorizes locks into ORPHANED_LOCKS_DETECTED (pull request is closed, merged, or missing), STALE_LOCKS_DETECTED (open pull request inactive beyond stale_lock_hours), ALL_LOCKS_ACTIVE (all locks correspond to active PRs), or NO_LOCKS_HELD (no locks for the repository), or LOOKUP_ERRORS (a pull request could not be verified).
  3. evaluate_reconciliation_verdict (io.kestra.plugin.core.flow.Switch) routes downstream actions based on the classified verdict:
    • Orphaned locks: If auto_unlock_orphans is enabled, release_orphaned_locks (io.kestra.plugin.core.flow.Loop) iterates across each orphaned lock, calls DELETE /locks?id=<lock name>, logs the release, and posts a summary card to Slack. With auto_unlock_orphans: false (the default) it only reports them.
    • Stale locks: Posts an alert to Slack warning that active pull requests have been idle past SLA while holding exclusive infrastructure locks.
    • Lookup errors: Fails the execution without releasing anything.
    • Clean state: Logs confirmation that all locks correspond to active development.

What you get

  • Automated elimination of Terraform pipeline deadlocks caused by abandoned or externally merged pull requests.
  • Orphaned locks released through Atlantis itself, which discards the plan and comments on the pull request, once you enable auto_unlock_orphans.
  • Proactive visibility into stale pull requests hoarding exclusive deployment locks across infrastructure environments.
  • Comprehensive Slack audit trail detailing who held the lock, which project was unlocked, and the associated PR number.

Who it's for

  • Platform and Site Reliability Engineers managing central Atlantis Terraform infrastructure.
  • Cloud Infrastructure Teams collaborating on monorepo Terraform directories.
  • DevOps Leads standardizing GitOps pull request hygiene and continuous delivery gates.

Why orchestrate this with Kestra

Without automated orchestration, teams rely on reactive Slack complaints ("Who has the lock on prod VPC?") and manual admin console sessions to unblock deploys. Implementing this in Kestra provides:

  • Safe Defaults: Reports by default, releases only when enabled, and refuses to release anything when GitHub cannot be queried.
  • Visual Audit Topology: Clear visualization of which locks were inspected, which were orphaned, and when HTTP DELETE commands were dispatched.
  • Declarative Secrets Masking: Secure handling of Atlantis and GitHub API tokens without exposing credentials in shell histories or log files.

Prerequisites

  • A running Atlantis server reachable over HTTP/HTTPS from Kestra workers.
  • A GitHub token that can read pull requests in github_repository.
  • Atlantis started with --web-basic-auth, --web-username and --web-password (the secrets below), as /api/locks and /locks sit behind the web UI's basic auth.
  • A Slack Incoming Webhook URL configured for DevOps notifications.

Secrets

  • ATLANTIS_WEB_USERNAME and ATLANTIS_WEB_PASSWORD: Atlantis web basic-auth credentials.
  • GITHUB_TOKEN: GitHub token used to query pull request metadata.
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook URL used to dispatch lock release audits.

Quick start

  1. In your Kestra namespace, configure the required secrets: ATLANTIS_WEB_USERNAME, ATLANTIS_WEB_PASSWORD, GITHUB_TOKEN and SLACK_WEBHOOK_URL.
  2. Import this blueprint into your Kestra workspace.
  3. Execute the flow manually with your atlantis_url and github_repository and review the report.
  4. Set auto_unlock_orphans: true to release orphaned locks, and add a Schedule trigger with an inputs: block to run it periodically.

How to extend

  • PR Comment Notifications: Add an io.kestra.plugin.github.issues.Comment task to automatically comment on the closed PR informing the author that their Atlantis lock was reclaimed.
  • GitLab & Bitbucket Support: Swap the GitHub REST API query in the Python task for GitLab Merge Request (/api/v4/projects/:id/merge_requests/:iid) or Bitbucket Server endpoints.
  • Multi-Repository Sweeps: Run the flow once per repository with a Schedule trigger that sets inputs:, or call it as a subflow in a Loop.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.