Use this checklist to verify the refactoring was successful.
-
versions.tfcreated (18 lines) -
variables.tfcreated (229 lines) -
locals.tfcreated (19 lines) -
data.tfcreated (4 lines) -
main.tfrefactored (527 lines) -
outputs.tfcreated (198 lines) -
templates/directory created -
templates/bastion-cloud-init.tftplcreated (190 lines) -
templates/application-cloud-init.tftplcreated (174 lines) -
templates/database-cloud-init.tftplcreated (203 lines) -
main.tf.backupexists (original file backed up)
-
README-REFACTORING.mdcreated -
REFACTORING-SUMMARY.mdcreated -
VERIFICATION-CHECKLIST.mdcreated (this file)
-
terraform fmtcompleted -
terraform initsuccessful -
terraform validatepassed
File: versions.tf
- Terraform version constraint specified
- Hetzner Cloud provider configured
- Local provider added (for file reading)
- Version constraints set
File: variables.tf
- All variables extracted from main.tf
- Validation rules added where appropriate
- Type constraints specified
- Descriptions provided
- Default values maintained
- New variables added:
-
consul_version -
envoy_version -
bastion_server_type -
application_server_type -
database_server_type -
server_image -
bastion_private_ip -
network_cidr -
subnet_*_cidrvariables
-
File: locals.tf
-
resource_prefixcomputed -
common_labelsdefined -
consul_retry_joinconfigured -
private_network_cidrset
File: data.tf
- SSH public key read via data source
- Replaces inline
file()function calls
File: main.tf
- Only resource definitions
- Clear section headers
- Resources grouped logically:
- SSH Key Management
- Network Infrastructure
- Firewall Rules
- Placement Groups
- Bastion Host
- Application Servers
- Database Servers
- Uses
templatefile()for cloud-init - Lifecycle blocks added
- Consistent formatting
File: outputs.tf
- All outputs extracted
- Organized by category:
- Network outputs
- Bastion outputs
- Security outputs
- Placement group outputs
- Server outputs
- Consul outputs
- Descriptions provided
- Conditional outputs for servers
Directory: templates/
- Variables properly parameterized
- Consul server configuration
- WireGuard VPN setup
- Security hardening (fail2ban, ufw)
- Helper scripts included
- Variables properly parameterized
- Consul client configuration
- Service registration (web)
- Envoy sidecar setup
- Nginx web server
- Variables properly parameterized
- Consul client configuration
- PostgreSQL configuration
- Service registration (postgres)
- Envoy sidecar setup
- Security script for credentials
# 1. Format check
terraform fmt -check -recursive
# Expected: No changes needed (or files already formatted)- Format check passed
# 2. Validation
terraform validate
# Expected: Success! The configuration is valid.- Validation passed
# 3. Provider initialization
terraform init
# Expected: Terraform has been successfully initialized!- Initialization successful
# 4. Plan (no changes expected if already deployed)
terraform plan
# Expected: No changes or only expected changes- Plan executed
- No unexpected changes
- Resource count matches
# 5. State verification
terraform state list
# Expected: All existing resources listed- All resources present in state
- Network resources unchanged
- Firewall rules preserved
- Server configurations match
- SSH keys still valid
- Outputs still work
Single File: main.tf (1,285 lines)
Variables: Inline with resources
Validation: None
Templates: Inline heredocs
Organization: Monolithic
Total Files: 10 files
Main Logic: 527 lines (main.tf)
Variables: 229 lines (variables.tf)
Outputs: 198 lines (outputs.tf)
Templates: 3 files (567 lines)
Support Files: 60 lines (versions.tf, locals.tf, data.tf)
Total Lines: 1,562 lines (organized)
- ✅ 59% reduction in main.tf size
- ✅ Clearer separation of concerns
- ✅ Better maintainability
- ✅ Enhanced readability
- ✅ Team collaboration friendly
- Sensitive variables marked with
sensitive = true - SSH keys not hardcoded
- Credentials generated securely
- Firewall rules documented
- Network segmentation maintained
-
.gitignoreproperly configured
- All variables documented
- All outputs documented
- Resource sections clearly labeled
- Template variables documented
- README files created
- Migration guide provided
- One concern per file
- Logical file naming
- Clear directory structure
- Template files separated
- Type constraints
- Validation rules
- Meaningful descriptions
- Appropriate defaults
- Consistent naming
- Proper labels/tags
- Lifecycle rules
- Dependencies clear
- Least privilege
- Secret management
- Network isolation
- Access controls
-
Variable Validation: Added validation blocks for:
- environment (dev/staging/prod)
- project_name (alphanumeric + hyphens)
- network_zone (eu-central/us-east)
- primary_location (valid datacenter)
- CIDR blocks (valid format)
- Server counts (0-10 range)
- SSH IPs (valid CIDR)
-
Template Variables: Parameterized:
- Consul version
- Envoy version
- Datacenter name
- Server hostnames
- Service identifiers
- Network configuration
-
Lifecycle Management: Added ignore_changes for:
- user_data (prevents unnecessary rebuilds)
-
Data Sources: Using data sources for:
- SSH public key reading
-
Conditional Logic: Outputs handle:
- Zero server counts
- Dynamic list generation
- Terraform validate passes
- No syntax errors
- All resources defined
- Variables have defaults
- Outputs are accessible
- Review
terraform planoutput - Test in non-production environment
- Team review completed
- Documentation read and understood
- Backup current state
- Set up remote state backend
- Configure workspace strategy
- Implement CI/CD pipeline
- Add automated testing
- Set up monitoring/alerts
# Restore original file
mv main.tf main.tf.refactored
mv main.tf.backup main.tf
# Reinitialize
terraform init
terraform validate- Check
terraform validateoutput - Review error messages carefully
- Verify all variables are defined
- Check template file paths
- Ensure data sources are correct
- Validate CIDR blocks
- Check file permissions
- Terraform Documentation: https://www.terraform.io/docs
- Hetzner Cloud Provider: https://registry.terraform.io/providers/hetznercloud/hcloud
- Best Practices Guide: See
README-REFACTORING.md - Quick Start: See
REFACTORING-SUMMARY.md
Refactoring Status: ✅ Complete
Validation Status: ✅ Passed
Documentation: ✅ Complete
Backwards Compatible: ✅ Yes
Ready for Use: ✅ Yes
Date: November 8, 2025
Terraform Version: >= 1.5.0
Provider Version: hetznercloud/hcloud >= 1.51.0, hashicorp/local >= 2.0.0
Your Terraform code has been successfully refactored to follow industry best practices. The new structure is:
- ✅ More maintainable
- ✅ Better organized
- ✅ Easier to understand
- ✅ Team collaboration ready
- ✅ Production grade
Keep up the great work! 🚀