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
Backend Limitations
Not all Terraform backends support saving planfiles locally with the -out flag. This is a limitation of Terraform and OpenTofu core, not Atmos. Atmos works within these constraints and provides workarounds where possible.
Understanding Backend Types
Terraform backends fall into two categories regarding planfile support:
- Standard Backends: Store state only, support local planfile generation
- Enhanced Backends: Manage both state and operations remotely, don't support local planfiles
When using -backend-config or hardcoding backend credentials in your configuration, Terraform embeds these values in plan files. This creates serious security risks:
- Credentials Exposed: Sensitive data like usernames, passwords, and tokens are stored in plaintext within the planfile
- CI/CD Breakage: Job-specific or time-limited tokens become invalid between plan and apply stages
- Compliance Violations: Planfiles containing secrets cannot be safely stored, shared, or committed to version control
The HTTP backend is particularly problematic: While it technically supports terraform plan -out, the embedded credentials make it effectively unusable in any production or CI/CD environment. For all practical purposes, treat the HTTP backend as not supporting planfiles.
Best Practice: Use environment variables for backend authentication or choose backends that support secure credential management.
Backends That DO NOT Support Local Planfiles
The following backends have limitations with terraform plan -out=<file>:
| Backend | Type | Actual Limitation | Recommended Approach |
|---|---|---|---|
| Remote Backend | Enhanced | Does not support saving execution plans locally. Returns error: "The 'remote' backend does not support saving the generated execution plan locally" | Use remote execution mode or migrate to standard backend with skip_planfile: true in Atmos |
| Terraform Cloud | Enhanced | Remote execution mode doesn't allow local plan output | Use VCS-driven workflow or configure skip_planfile: true in Atmos |
| Terraform Enterprise | Enhanced | Same as Terraform Cloud - remote execution limitation | Use VCS-driven workflow or configure skip_planfile: true in Atmos |
| HTTP Backend | Standard | ⚠️ Security Risk: While technically supports -out, backend credentials are embedded in planfiles making it effectively unusable | Do not use planfiles with HTTP backend. Use environment variables for auth or switch backends |
Backends That Support Planfiles
All standard backends support local planfile generation. These are the most commonly used:
| Backend | Storage Type | Best For | Key Features |
|---|---|---|---|
| S3 | AWS S3 | AWS infrastructure | • DynamoDB for locking • Encryption at rest • Versioning support |
| GCS | Google Cloud Storage | GCP infrastructure | • Native state locking • Customer-managed encryption keys • Versioning support |
| Azure Blob | Azure Storage | Azure infrastructure | • Native locking via blob leases • Encryption at rest • Soft delete capability |
| Local | Filesystem | Development/testing | • Simple setup • No external dependencies • Not for production teams |
| Consul | HashiCorp Consul | Multi-cloud setups | • Distributed storage • High availability |