The terraform-wrapper.sh script automatically manages the terraprem backend by:
- Detecting
backend "terraprem"in your Terraform configuration - Starting
kubectl port-forwardautomatically - Temporarily rewriting the config to use
backend "remote" - Running the terraform command
- Restoring the original configuration
- Stopping the port-forward
Result: Users just run terraform init and everything works automatically!
# Backup existing terraform (optional)
mv ~/.local/bin/terraform ~/.local/bin/terraform-real
# Install wrapper
cp scripts/terraform-wrapper.sh ~/.local/bin/terraform
chmod +x ~/.local/bin/terraform
# Ensure ~/.local/bin is in PATH
export PATH="$HOME/.local/bin:$PATH"# Add to ~/.bashrc or ~/.zshrc
alias terraform='$HOME/Documents/Projects/TfCloudCopy/scripts/terraform-wrapper.sh'ln -s $HOME/Documents/Projects/TfCloudCopy/scripts/terraform-wrapper.sh ~/.local/bin/terraformIn your main.tf (or any .tf file):
terraform {
backend "terraprem" {
k8s_context = "your-k8s-context" # Required
namespace = "terraprem" # Required (default: "terraprem")
service = "terraprem-backend" # Required
local_port = 8080 # Optional (default: 8080)
remote_port = 8080 # Optional (default: 8080)
organization = "terraprem" # Required
workspaces {
name = "my-workspace" # Required
}
}
}Just run terraform commands normally:
terraform init
terraform plan
terraform applyThe wrapper automatically:
- Detects the
backend "terraprem"configuration - Starts
kubectl port-forward - Rewrites config to
backend "remote" - Runs the terraform command
- Restores original config
- Stops port-forward
The wrapper scans all .tf files in the current directory for backend "terraprem" blocks.
- Starts
kubectl port-forward svc/<service> <local_port>:<remote_port> -n <namespace> --context <k8s_context> - Waits for port to be ready (checks with
ncorcurl) - Stores PID in
/tmp/terraprem-port-forward.pid - Stops on script exit (handles Ctrl+C gracefully)
- Backup: All
.tffiles are backed up to/tmp/terraprem-backup-<timestamp>/ - Rewrite: Backend block is rewritten from
backend "terraprem"tobackend "remote"with:hostname = "localhost:<local_port>"organization = "<organization>"workspaces { name = "<workspace>" }
- Execute: Terraform runs with the rewritten config
- Restore: Original files are restored from backup
The wrapper always cleans up on exit:
- Stops port-forward process
- Restores original
.tffiles - Removes backup directories
- Preserves terraform exit code
k8s_context- Kubernetes context nameservice- Kubernetes service nameorganization- Terraform organizationworkspace- Workspace name (inworkspaces { name = "..." })
namespace- Kubernetes namespace (default: "terraprem")local_port- Local port for port-forward (default: 8080)remote_port- Remote port on service (default: 8080)
The wrapper handles various error scenarios:
- Missing required fields: Shows which field is missing
- Port already in use: Checks before starting port-forward
- kubectl not found: Validates kubectl is installed
- Port-forward fails: Shows error and exits
- Config parsing fails: Shows parsing error
- Interrupt (Ctrl+C): Cleans up everything gracefully
If you see "Port X is already in use":
# Check what's using the port
lsof -ti:8080
# Kill the process
kill $(lsof -ti:8080)Make sure kubectl is installed and in PATH:
which kubectl
kubectl version --client- Ensure
backend "terraprem"is in a.tffile in the current directory - Check for typos:
backend "terraprem"(notbackend "terraprem"orbackend terraprem) - Ensure the backend block is inside a
terraform {}block
Check the logs:
cat /tmp/terraprem-port-forward.logVerify Kubernetes access:
kubectl get svc -n terraprem terraprem-backendIf something goes wrong, backups are in /tmp/terraprem-backup-*/:
ls -la /tmp/terraprem-backup-*terraform {
backend "terraprem" {
k8s_context = "my-context"
namespace = "terraprem"
service = "terraprem-backend"
organization = "terraprem"
workspaces {
name = "prod"
}
}
}terraform {
backend "terraprem" {
k8s_context = "my-context"
namespace = "terraprem"
service = "terraprem-backend"
local_port = 9090 # Custom local port
remote_port = 8080
organization = "terraprem"
workspaces {
name = "prod"
}
}
}# Terminal 1
kubectl port-forward -n terraprem svc/terraprem-backend 8080:8080
# Terminal 2
terraform init
terraform apply# Just one command!
terraform init
terraform applykubectl- Must be in PATHterraform- Real terraform binary (found automatically)ncorcurl- For port checking- Standard Unix tools:
grep,sed,awk
- HCL Parsing: Uses simple regex/awk parsing. Complex HCL with unusual formatting may not parse correctly.
- Single Backend: Only handles one
backend "terraprem"block per directory. - Port Conflicts: Doesn't handle port conflicts automatically (shows error).
- Multiple Terraform Commands: Port-forward stays alive between commands (by design).
- Better HCL parsing (use HCL library)
- Handle multiple backend blocks
- Auto-detect and reuse existing port-forwards
- Support for backend config in separate files
- Better error messages with line numbers