terragrunt
terragrunt copied to clipboard
Calling apply does not remove `-var` arguments if the actual variables are a separate argument
I am using Terragrunt v0.21.11
If I say:
terraform {
extra_arguments "common_vars" {
commands = get_terraform_commands_that_need_vars()
arguments = [
"-var", foo=bar",
]
}
}
then run terragrunt with terragrunt apply /a/plan, then it will call terraform like: terraform foo=bar. It seems to correctly remove the `-var' argument because you don't pass them if you're doing an apply with a plan file. However it does not remove the actual variables.
A workaround is to do the following:
terraform {
extra_arguments "common_vars" {
commands = get_terraform_commands_that_need_vars()
arguments = [
"-var=foo=bar",
]
}
}
The terragrunt successfully removes that entire argument from the terraform arguments list.
It would be nice to at least get a warning if the first form is used (i.e. if a "-var" flag is detected which exactly matches -var, print an error saying "please use -var=foo=bar instead")
Adding a warning or error when we see -var in the arguments list makes sense. We likely won't get to this any time soon, but a PR to implement that suggestion is very welcome.
Hi @ayqazi! I'm trying to reproduce this behavior but I can't.
In both scenarios the output is right as expected.
$> terragrunt --version
terragrunt version v0.21.11
$> terraform --version
Terraform v0.12.19
$> cat ./main.tf
variable "foo" {}
output "output_foo" {
value = var.foo
}
1st scenario:
$> cat ./terragrunt.hcl
terraform {
extra_arguments "common_vars" {
commands = get_terraform_commands_that_need_vars()
arguments = [
"-var", "foo=bar",
]
}
}
terragrunt apply (1st scenario) output:
$> terragrunt apply
[...] (terragrunt logs and terraform initialization) [...]
[terragrunt] 2020/05/27 22:39:54 Running command: terraform apply -var foo=bar
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
output_foo = bar
2nd scenario:
$> cat ./terragrunt.hcl
terraform {
extra_arguments "common_vars" {
commands = get_terraform_commands_that_need_vars()
arguments = [
"-var=foo=bar",
]
}
}
terragrunt apply (2nd scenario) output:
$> terragrunt apply
[...] (terragrunt logs and terraform initialization) [...]
[terragrunt] 2020/05/27 22:43:38 Running command: terraform apply -var=foo=bar
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
output_foo = bar
What am I missing here on this issue?
Try this:
main.tf:
variable "foo" {}
terragrunt.hcl;
terraform {
extra_arguments "common_vars" {
commands = get_terraform_commands_that_need_vars()
arguments = [
"-var", "foo=bar"
]
}
Run these commands:
terragrunt plan -out /tmp/plan.tfplan
terragrunt apply /tmp/plan.tfplan
Output from apply:
[terragrunt] 2020/05/28 05:40:54 Running command: terraform apply foo=bar ../plan.tfplan
Too many command line arguments. Configuration path expected.
[terragrunt] 2020/05/28 05:40:54 Hit multiple errors:
Terraform v0.12.25
terragrunt version v0.23.23
terragrunt.hcl:
terraform {
extra_arguments "common_vars" {
commands = get_terraform_commands_that_need_vars()
optional_var_files = [
"${get_parent_terragrunt_dir()}/terraform.tfvars",
"${get_parent_terragrunt_dir()}/common.tfvars",
"${find_in_parent_folders("region.tfvars", "skip-env-if-does-not-exist")}",
"${find_in_parent_folders("env.tfvars", "skip-env-if-does-not-exist")}",
]
}
}
terragrunt apply -no-color plan.out
[terragrunt] 2020/06/04 12:32:37 Running command: terraform apply -var-file=/a/b/c/eu-west-1/region.tfvars -var-file=/a/b/c/eu-west-1/stage/env.tfvars -input=false -no-color plan.out
Error: Can't set variables when applying a saved plan
The -var and -var-file options cannot be used when applying a saved plan file,
because a saved plan includes the variable values that were set when it was
created.
[terragrunt] 2020/06/04 12:32:37 Detected 1 Hooks
[terragrunt] 2020/06/04 12:32:37 Hit multiple errors:
exit status 1
List returned by get_terraform_commands_that_need_vars() contains apply, it should detect applying from plan file.
Still a issue, it would be great if that would be at least hinted in the docs.
My workaround is to avoid the args at all and just generate a tfvars to avoid unwanted mutation of the files (at least in my case encode/decode json to input screwed up objects):
generate "config_tfvars" {
path = "configs.auto.tfvars.json"
if_exists = "overwrite"
disable_signature = true
contents = file("${get_repo_root()}/terraform/config/${local.environment}.json")
}