Terraform Planfiles
Atmos provides sophisticated planfile management for Terraform, enabling safe and predictable infrastructure changes through plan generation, storage, and application workflows.
Overview
A Terraform planfile is a binary file that contains the exact set of changes Terraform will make to your infrastructure. Atmos enhances Terraform's native planfile functionality by:
- Standardizing planfile naming and storage
- Enabling seamless plan-and-apply workflows
- Supporting both automatic and custom planfile management
- Integrating with CI/CD pipelines and approval workflows
Why Planfiles Are Important
Planfiles are one of Terraform's most powerful features for ensuring safe, predictable infrastructure changes. They solve a critical problem: ensuring that what you reviewed is exactly what gets applied.
The Problem Without Planfiles
Without planfiles, there's a dangerous gap between plan and apply:
- You run
terraform planand review the changes - Time passes (minutes, hours, or days)
- During
terraform apply:- Someone else might have modified the infrastructure
- Configuration files might have changed
- Provider APIs might return different data
- Remote state might have been updated
- Result: The applied changes differ from what was reviewed!
How Planfiles Solve This
A planfile is a snapshot that captures:
- The exact infrastructure state at planning time
- The precise changes Terraform will make
- All variable values and provider configurations
- The complete dependency graph
When you apply a planfile, Terraform uses this snapshot instead of re-calculating changes, guaranteeing that what you reviewed is what gets applied.
Benefits of Using Planfiles
1. Guaranteed Consistency
- No Drift: Changes applied match exactly what was planned, even if infrastructure or configuration changed
- Immutable Plans: Once generated, a planfile cannot be modified, ensuring integrity
- State Version Tracking: Planfiles capture the state version at plan time to detect if the state has changed before applying. Note that actual concurrency protection comes from your backend's state locking mechanisms, not from planfiles themselves
2. Enhanced Security and Compliance
- Audit Trail: Planfiles serve as immutable records of approved changes for compliance
- Review Process: Security teams can analyze planfiles before approval
- Approval Workflows: Enable separation of duties - developers plan, ops team approves and applies
- Policy Validation: Run security scanners (Checkov, tfsec, OPA) against planfiles. Use
atmos terraform generate planfileto create the JSON version needed by most security scanning tools.
3. CI/CD Pipeline Safety
- Atomic Operations: Apply exactly what passed through your CI/CD tests
- Artifact Storage: Store planfiles in external storage (e.g., S3, GitHub Actions artifacts). Atmos provides a planfile storage GitHub Action for this purpose. The native
github/artifactsstore talks to the GitHub Artifacts API directly, so inside GitHub Actions you must surface the runner's runtime credentials with thegithub-runtimeaction — see Planfile Storage - Change Documentation: Saved planfiles provide a record of what changes were planned, useful for understanding what was deployed (not for automated rollback)
- Multi-Stage Pipelines: Separate plan generation, validation, approval, and application stages
4. Operational Efficiency
- Faster Apply: No need to refresh state or recalculate the dependency graph (can be 50-70% faster)
- Reduced API Calls: Fewer cloud provider API calls during apply (important for rate limits)
- Predictable Timing: Apply duration is predictable since the work is pre-calculated
- Offline Review: Planfiles can be converted to JSON/YAML for review without Terraform access
5. Team Collaboration
- Async Workflows: One engineer can generate plans for another to review and apply
- Change Windows: Generate plans ahead of maintenance windows, apply during the window
- Knowledge Transfer: Planfiles document infrastructure changes for team learning
Planfile Limitations and Challenges
While planfiles provide significant benefits, they also have important limitations to understand:
Planfile Staleness
Planfiles become stale and must be recreated if:
- Infrastructure changes externally (e.g., manual changes, other automation)
- Terraform state is updated by other operations
- Configuration files are modified after plan generation
- Provider data sources return different values
Plan Comparison and Verification
When recreating planfiles, you cannot automatically verify they're identical to the original. Use atmos terraform plan-diff to compare plans and determine if they are effectively the same or have material differences.
# Generate new planfile
atmos terraform plan vpc -s dev -out vpc-dev.planfile
# Compare with previous plan to detect drift
atmos terraform plan-diff vpc -s dev
IAM Role and Authentication Issues
If different IAM roles are used for plan vs apply, the planfile will cause failures:
- Planfiles embed the IAM role used during plan generation (via provider
assume_roleconfiguration) - If you apply with a different IAM role, Terraform will fail because the embedded credentials don't match
- Solution: Use the same IAM role for both plan and apply, or regenerate the planfile with the apply role
Regeneration and Re-verification
When any of these issues occur:
- Regenerate the planfile with current state and credentials
- Re-verify the plan matches expectations (use
plan-diffif comparing to a previous plan) - Re-run approval workflows if your process requires it