Terraform Workflows¶
Terraform follows a predictable, iterative workflow designed for consistency, safety, and collaboration.
Core Terraform Workflow¶
Terraform operates through three main steps:
- Write — Define infrastructure using code
- Plan — Preview changes before applying
- Apply — Provision reproducible infrastructure
Team Workflows — Individual Practitioner¶
I. Write
- Develop Terraform configuration locally
- Store code in a Git repository
- Use
terraform planandterraform validatefor rapid feedback - Tight edit → test → fix cycle
II. Plan
- Commit changes to your local Git repo
- Often use a single branch (e.g.,
main) for simplicity
III. Apply
- Run
terraform applylocally, review the execution plan - Approve and wait for provisioning
- Push commits to the remote Git repository after a successful apply
Team Workflows — Team-Based¶
I. Write
- Each engineer writes Terraform code locally
- Use separate Git branches per feature
- Smaller teams run
terraform planlocally - Larger teams rely on CI/CD pipelines to avoid distributing credentials
- Maintain fast local testing cycles when possible
II. Plan
- Open a Pull Request
- CI/CD generates and
discardsan execution plan for PR review
III. Apply
- Approve → Merge → CI/CD performs apply
Challenges:
- CI/CD pipeline complexity
- State management coordination
- Different access controls for
planandapply - Secrets handling
- Multi-environment management
Team Workflows — Terraform Cloud¶
I. Write
- Terraform Cloud is used as the remote backend
- Input variables stored securely in Terraform Cloud
- Built-in VCS (GitHub/GitLab/Bitbucket) integration for CI/CD
- Developers write code normally using branches
II. Plan
- PR opened → Terraform Cloud automatically generates the execution plan
III. Apply
- Approve PR → Merge → Terraform Cloud performs apply
- Team members can manually confirm applies when required
Advantages:
- Simplifies CI/CD setup
- Secure credentials & sensitive variable management
- Easy audit logs and run history
Terraform Troubleshooting¶
Error types¶
- Language → syntax errors for the terraform/hcl language (
terraform fmt,validate) - State → resource drift (
refresh,apply,-replace) - Core → internal bugs of the core library (
TF_LOG) - Provider → API changes or edge cases (
TF_LOG)
Debugging¶
Terraform has detailed logs which can be enabled by setting the TF_LOG environment
variable (TRACE, DEBUG, INFO, WARN, ERROR, JSON - same as TRACE or higher, and uses
parseable JSON encoding as the formatting).
Logging can be enabled separately (same values) for:
TF_LOG_CORETF_LOG_PROVIDER
TF_LOG_PATH environment variable can be used to log to a specific file.
Crash Log¶
If terraform ever crashes (a "panic" in the Go runtime), it saves a log file with
the debug logs from the session as well as the panic message and backtrace to
crash.log. This log file is meant to be parssed along to the Terraform developers
via a GitHub issue.