terragrunt icon indicating copy to clipboard operation
terragrunt copied to clipboard

Calling apply does not remove `-var` arguments if the actual variables are a separate argument

Open ayqazi opened this issue 5 years ago • 5 comments

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")

ayqazi avatar Feb 06 '20 16:02 ayqazi

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.

yorinasub17 avatar Feb 06 '20 17:02 yorinasub17

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?

edgarsandi avatar May 28 '20 01:05 edgarsandi

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:

ayqazi avatar May 28 '20 04:05 ayqazi

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.

akurtasinski avatar Jun 04 '20 13:06 akurtasinski

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")
}

project0 avatar Jan 22 '24 13:01 project0