1. Workflow Execution
- Initialize:
terraform init. Run this when you add a new provider or module.
- Format:
terraform fmt. Automatically formats your code to HCL standards.
- Validate:
terraform validate. Checks for syntax errors.
- Plan:
terraform plan -out=tfplan. Always review the plan before applying. Check for accidental "Destroy" actions.
- Apply:
terraform apply tfplan. Apply the changes.
2. State Management
- List Resources:
terraform state list. Shows all resources currently tracked in the state file.
- Show Details:
terraform state show <resource_name>. Inspect the attributes of a specific resource in the state.
- Unlock State: If a Terraform run crashes or is interrupted, the state might remain locked.
terraform force-unlock <lock_id> (Use with extreme caution!).
3. Debugging
- Enable Logs: Set the environment variable
TF_LOG=DEBUG or TF_LOG=TRACE to see detailed logs of what Terraform is doing (e.g., API calls to AWS).
- PowerShell:
$env:TF_LOG="DEBUG"
- Bash:
export TF_LOG=DEBUG
4. Module Management
- Update Modules: If you are using remote modules (e.g., from GitHub), run
terraform get -update to pull the latest versions.