Open ryanm101 opened 4 years ago
Work around: common.auto.tfvars.json
import os
def pretf_variables():
BACKEND = os.getenv('TFENV', "dev")
# ENV Specific
print(f"Loading Vars: {BACKEND}")
if BACKEND == "dev":
yield {
"environment": "dev",
}
elif BACKEND == "prd":
yield {
"environment": "prd",
}
I tried this and it worked as expected. It doesn't load values from dev.tfvars.py
(or dev.tfvars.json
) automatically. Try running it with -var-file=dev.tfvars.json
or renaming dev.tfvars.py
to dev.auto.tfvars.py
or terraform.tfvars.py
so it gets loaded automatically.
The variable loading logic in Pretf should match what is described here, in particular the variable definition precedence section at the bottom shows how Terraform works. I'm pretty sure that Pretf is working correctly and in line with how Terraform itself handles variables.
I'm closing this because I think it's working correctly. Feel free to reopen if you think I've got it wrong.
@raymondbutcher you cant use -var-file=
with init
The issue is that pretf builds all the json 1st even though in the init stage the only relevant part is the backend.
Ah, interesting.
I ran terraform init -var-file=dev.tfvars.json
and it worked. Terraform seems to ignore this argument, at least right now, but I wouldn't blame them if they made it more strict in the future and it stopped working.
I use Pretf to dynamically generate the backend configuration that terraform init
uses. I use variables to generate it.
It's getting late here so I'll think about this more another time.
No worries, I'm on a plane anyway. :-)
Added my backend.tf.py to the original description (forgot to include it which is likely what caused the initial confusion)
The following works fine if i move 'environement' into
common.auto.tfvars.py
backend.tf.py
common.auto.tfvars.py
dev.tfvars.py
s3.tf.py
variables.tf.py