Skip to content
Snippets Groups Projects
Select Git revision
  • cce6a4d4b0213ba73c5319766a8a428a84e417e8
  • main default
  • v0.3.1
  • v0.3.0
  • v0.2.0
  • v0.1.0
6 results

tf

Adar Nimrod's avatar
nimrod authored
We can't add -var-file to an apply command if a plan is specified.
cce6a4d4
History

tf

pipeline status

A simple Terraform wrapper to use variable definition files if they match the workspace name.

Rationale

With workspaces, one can use the same module in different environments with minor changes like different domain name, size of a cluster, instance type. One can use variable definition files to store the values for each workspace in a dedicated file. This wrapper replaces the following:

terraform workspace select prod
terraform plan -var-files=prod.tfvars -out tfplan

to:

terraform workspace select prod
tf plan -out tfplan

Installation

python3 -m pip install terraformation

The wrapper is a single Python3 script with no external dependencies. If you prefer, you can download the tf.py and use that instead.

Usage

Replace terraform with tf. In case there's a variable definitions file (that ends with .tfvars) that matches the current workspace name (if the current workspace name is prod and a file named prod.tfvars exists) than a -var-file=prod.tfvars argument is added to the relevant commands (like plan and import). All other arguments are kept as they were. Similarly, if a directory exists with the same name as the workspace, for all the files inside that directory that end with .tfvars, a -var-file argument is added. For example: -var-file=prod/a.tfvars and -var-file=prod/b.tfvars.

By default tf invokes terraform, but if you're using a different tool (like OpenTofu you can set the TF_CLI environment variable to that tool name. If you wish to know the exact command tf is running set the TF_DEBUG environment variable to 1 and the command will printed before the it's invoked.

License

This software is licensed under the MIT license (see the LICENSE.txt file).

Author

Nimrod Adar, contact me or visit my website. Patches are welcome via git send-email. The repository is located at: https://git.shore.co.il/nimrod/.