Tools for managing root configuration repos for multi-environment or multi-service cloud infrastructure.
Root configurations or root modules are the working directories from which you run the Terraform CLI. ℹ️
findtfvars
: Recursively finds .tfvars files in parent directories
tfvars-to-args
: Converts a list of .tfvars files to Terraform command-line arguments.
Find all .tfvars
files in parent directories and convert to Terraform arguments:
findtfvars | tfvars-to-args
Use in a Terraform command:
TF_ARGS=$(findtfvars | tfvars-to-args)
terraform plan ${TF_ARGS}
Whether your root config repo is structured as a
Monorepo
repo-per-environment
repo-per-team
repo-per-service
your configurations will fall into two categories:
-
Environment Independent
- Variables may be set at any parent directory common to all environments
- Organizational hierarchies can set variables to standardize cloud estate
-
Environment Specific
- Variables should be set in the environment directory
- Values take precedence over those established generally
tfvars-tools
simplifies the management of Terraform variables across different organizational structures and environments, ensuring consistency and flexibility in your cloud infrastructure configurations, while
Keeping your Infra Repo DRY .
Consider this multi-service / multi-environment monorepo:
/org
├── common.tfvars
├── service1
│ ├── service1-common.tfvars
│ ├── dev
│ │ ├── main.tf
│ │ └── dev.tfvars
│ └── prod
│ ├── main.tf
│ └── prod.tfvars
└── service2
├── service2-common.tfvars
├── dev
│ ├── main.tf
│ └── dev.tfvars
└── prod
├── main.tf
└── prod.tfvars
To apply Terraform for the dev environment of service1 (and keep your sanity):
cd /org/service1/dev
terraform apply $(findtfvars | tfvars-to-args)
This will apply variables in this order, with conflicts being resolved by favoring the most recent assignment:
/org/common.tfvars
/org/service1/service1-common.tfvars
/org/service1/dev/dev.tfvars
Default use-case, Terraform
TF_ARGS=$(findtfvars | tfvars-to-args)
terraform plan ${TF_ARGS}
With Custom format
TF_ARGS=$(findtfvars -f ".toml" | tfvars-to-args -format="--file=%s")
some_other_tool ${TF_ARGS}
With verbose output
TF_ARGS=$(findtfvars | tfvars-to-args -verbose)
echo "Arguments: ${TF_ARGS}"
terraform plan ${TF_ARGS}
findtfvars [OPTIONS]
OPTIONS:
-d, --dir DIR Start searching from DIR (default: current directory)
-h, --help Show this help message
tfvars-to-args [OPTIONS]
OPTIONS:
-r, --reverse Output args in the order they were found (default: reverse order)
-h, --help Show this help message
Environment variables:
FINDTFVARS_MAX_DEPTH
: Maximum directory depth to search (default: unlimited)TFVARS_TO_ARGS_PREFIX
: Prefix for generated arguments (default: "-var-file=")
Config file: ~/.tfvars-toolsrc
max_depth=10
arg_prefix="--var-file="
For those who prefer their tools like their coffee - instant and strong:
# For Linux
curl -Lo tfvars-tools.tar.gz https://github.com/iancaseydouglas/tfvars-tools/releases/latest/download/tfvars-tools_Linux_x86_64.tar.gz
# For macOS
curl -Lo tfvars-tools.tar.gz https://github.com/iancaseydouglas/tfvars-tools/releases/latest/download/tfvars-tools_Darwin_x86_64.tar.gz
# Extract, make executable, and move to a directory in your PATH
tar xzf tfvars-tools.tar.gz
chmod +x findtfvars tfvars-to-args
sudo mv findtfvars tfvars-to-args /usr/local/bin/
Now go forth and DRY up your tfvars.
So you like recursively defined functions, you say...
- Clone the repo
- Read the Makefile
make
your way to glory- Open PR
Many of the functions in use by Terragrunt or similar tools can be achieved with a wrapper around the tfvars-tools
.