Backend Types
The nx-terraform plugin supports two backend types for Terraform state storage. Choosing the right backend type is crucial for your infrastructure management strategy.
Overview
A Terraform backend determines where state is stored and how it's managed. The plugin supports:
- AWS S3 Backend - Production-ready remote state storage
- Local Backend - Simple local file storage
AWS S3 Backend
Production-ready remote state storage using AWS S3 with optional DynamoDB state locking.
Characteristics
- State Storage: AWS S3 bucket
- State Locking: DynamoDB table (optional but recommended)
- Versioning: Enabled by default
- Encryption: Enabled by default
- Access Control: IAM-based
- Team Collaboration: Full support
- Use Case: Production environments, team projects
Configuration
When creating a backend with AWS S3:
nx g nx-terraform:terraform-backend my-backend --backendType=aws-s3
Generated Infrastructure
The backend project creates:
-
S3 Bucket
- Versioning enabled
- Encryption enabled
- Object lock for state protection
- Dynamic bucket naming
-
DynamoDB Table (optional)
- Used for state locking
- Prevents concurrent modifications
- Ensures state consistency
Backend Configuration
After applying, backend.config is generated:
bucket = "my-workspace-terraform-setup-abc123"
key = "terraform-setup/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-setup-lock"
encrypt = true
Requirements
- AWS account
- AWS credentials configured
- IAM permissions:
s3:CreateBuckets3:PutObjects3:GetObjectdynamodb:CreateTable(if using locking)dynamodb:PutItemdynamodb:GetItem
Advantages
- ✅ Production-ready
- ✅ Supports team collaboration
- ✅ State locking prevents conflicts
- ✅ Versioning for state history
- ✅ Encryption at rest
- ✅ Scalable and reliable
Disadvantages
- ❌ Requires AWS account and credentials
- ❌ More complex setup
- ❌ Potential costs (S3 storage, DynamoDB)
Best Practices
- Enable State Locking: Always use DynamoDB for state locking in production
- Use Separate Buckets: Consider separate buckets for different environments
- Enable Versioning: Keep state history for recovery
- Access Control: Use IAM policies to restrict access
- Backup Strategy: Consider cross-region replication for critical state
Local Backend
Simple local file storage for Terraform state.
Characteristics
- State Storage: Local
terraform.tfstatefile - State Locking: None
- Versioning: None (manual backup required)
- Encryption: None (file system dependent)
- Access Control: File system permissions
- Team Collaboration: Limited (file conflicts)
- Use Case: Development, testing, single-user scenarios
Configuration
When creating a backend with local storage:
nx g nx-terraform:terraform-backend my-backend --backendType=local
Backend Configuration
The backend uses a simple local configuration:
terraform {
backend "local" {
path = "terraform.tfstate"
}
}
State File Location
State is stored in the project directory:
packages/terraform-setup/terraform.tfstate
Advantages
- ✅ Simple setup (no AWS required)
- ✅ No additional costs
- ✅ Fast (local file system)
- ✅ Good for development
- ✅ Easy to backup manually
Disadvantages
- ❌ No state locking (concurrent access issues)
- ❌ Not suitable for teams
- ❌ No automatic versioning
- ❌ Manual backup required
- ❌ File conflicts in shared environments
Best Practices
- Version Control: Add
*.tfstateto.gitignore(state may contain secrets) - Manual Backups: Regularly backup state files
- Single User: Use only for single-user scenarios
- Development Only: Don't use for production
- State Security: Be careful with state files (may contain sensitive data)
Comparison
| Feature | AWS S3 Backend | Local Backend |
|---|---|---|
| State Storage | S3 bucket | Local file |
| State Locking | Yes (DynamoDB) | No |
| Versioning | Yes (automatic) | No |
| Encryption | Yes (S3 encryption) | File system dependent |
| Team Support | Excellent | Poor |
| Setup Complexity | Medium | Low |
| Cost | Low (S3 storage) | Free |
| Scalability | High | Low |
| Production Ready | Yes | No |
| Use Case | Production, teams | Development, single user |
Choosing the Right Backend
Choose AWS S3 If:
- You're working in a team
- You need production-grade state management
- You want state locking
- You need state versioning
- You're deploying to AWS
Choose Local If:
- You're developing locally
- You're the only user
- You want the simplest setup
- You're just learning
- You don't have AWS access
Migration Between Backends
From Local to AWS S3
- Create a new AWS S3 backend project
- Apply the backend to create infrastructure
- Update stateful projects to use the new backend
- Migrate state (if needed):
terraform init -migrate-state
From AWS S3 to Local
- Update backend configuration to local
- Reinitialize:
terraform init -migrate-state - State will be copied to local file
Migrating state can be risky. Always backup state before migration.
Backend Configuration Files
backend.config (AWS S3)
Generated after backend apply:
bucket = "my-workspace-terraform-setup-abc123"
key = "terraform-setup/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-setup-lock"
encrypt = true
backend.tf (Stateful Projects)
References the backend configuration:
terraform {
backend "s3" {
config_file = "../../terraform-setup/backend.config"
}
}
Or for local:
terraform {
backend "local" {
path = "terraform.tfstate"
}
}
Security Considerations
AWS S3 Backend
- Use IAM roles with least privilege
- Enable encryption at rest
- Use bucket policies for access control
- Enable MFA delete (optional)
- Monitor access with CloudTrail
Local Backend
- Add
*.tfstateto.gitignore - Use file system permissions
- Encrypt sensitive state files
- Regular backups
- Don't commit state to version control
Troubleshooting
For backend-related issues, see the Troubleshooting Guide.
Related Topics
- Project Types - Learn about different project types
- Configuration - Understand backend configuration
- Terraform Backend Generator - Generator reference