Terraform GitHub Actions Pipeline Prompt
Design a production-grade GitHub Actions workflow for Terraform that runs fmt/validate/plan on PRs, posts plan summaries as comments, and gates apply behind environments and OIDC cloud auth.
- Target user
- Platform engineers wiring Terraform into GitHub Actions CI/CD
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior platform engineer who has built Terraform delivery pipelines on GitHub Actions for teams managing hundreds of stacks. Design a complete, secure workflow. I will provide: - Repo layout (mono-repo vs per-stack dirs, environments) - Cloud provider(s) and current auth method (static keys, OIDC, Vault) - Branch protection and reviewer requirements - Backend type (S3/GCS/azurerm/TFC) and locking setup - Current pain points (slow plans, secret leakage, apply races) Your job: 1. **Workflow topology** — decide PR-triggered `plan` vs `main`-triggered `apply`. Recommend two workflows (or one with conditional jobs) and explain the trade-off versus a single matrix job. 2. **OIDC cloud auth** — replace any static credentials with `id-token: write` + provider role assumption (`aws-actions/configure-aws-credentials`, Workload Identity Federation, or `azure/login`). Show the exact `permissions:` block and trust-policy conditions scoped to `repo:` and `ref:`. 3. **Plan stage** — `fmt -check`, `init` with backend config, `validate`, `plan -out=tfplan -lock-timeout=120s`, and upload the plan artifact. Show how to detect "no changes" and short-circuit. 4. **PR comment** — render `terraform show -no-color tfplan` into a collapsed `<details>` comment, with an add/change/destroy summary line at the top. Update the existing comment instead of stacking new ones. 5. **Apply gate** — use a GitHub Environment with required reviewers and the *exact same* plan artifact (never re-plan at apply time). Explain why re-planning at apply is dangerous. 6. **Concurrency & locking** — set `concurrency:` keyed by stack+environment to serialize runs and prevent two applies racing on the same state. 7. **Matrix for many stacks** — a path-filter step that computes the changed stacks and feeds a dynamic matrix, so only touched directories plan. 8. **Guardrails** — caching providers, pinning action SHAs, masking sensitive outputs, failing on `destroy` count above a threshold without an override label. Output as: (a) `.github/workflows/terraform-plan.yml`, (b) `terraform-apply.yml`, (c) the OIDC trust policy, (d) the dynamic-matrix script, (e) a rollout checklist for migrating off static keys. Bias toward: same-artifact apply, least-privilege OIDC, deterministic and serialized runs.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Terraform OIDC CI Authentication Design Prompt
Replace long-lived cloud credentials in Terraform CI pipelines with short-lived OIDC-federated identity.
-
Terraform Output Consumption Pipeline Prompt
Safely consume `terraform output -json` in downstream scripts, pipelines, and other tools without leaking secrets or coupling to fragile parsing.
-
Terraform Plan JSON to PR Comment Prompt
Turn raw `terraform show -json` plan output into a compact, scannable pull-request comment that a reviewer can approve or reject in under a minute.
-
Atlantis Server Setup & Workflow Prompt
Stand up a self-hosted Atlantis server for pull-request Terraform automation — repo allowlist, server-side workflows, autoplan, apply locks, and policy checks — with sane security defaults.
More Terraform prompts & error guides
Browse every Terraform prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.