gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
8.01k stars 971 forks source link

Need some way to specify where extra arguments are placed #454

Open chrisportela opened 6 years ago

chrisportela commented 6 years ago

For instance I add the following to all my terraform states

{
  terraform {
    extra_arguments "ouput_plan" {
      commands = [
        "plan"
      ]

      # With the get_tfvars_dir() function, you can use relative paths!
      arguments = [
        "-out=${get_tfvars_dir()}/tfplan.out"
      ]
    }

    extra_arguments "apply_plan" {
      commands = [
        "apply",
        "destroy"
      ]

      # With the get_tfvars_dir() function, you can use relative paths!
      arguments = [
        "${get_tfvars_dir()}/tfplan.out"
      ]
    }
  }
}

However when I run apply-all I get the following:

[snip]
[terragrunt] [/Users/cp/projects/work/infrastructure/src/aws/global] 2018/03/28 15:02:36 Running command: terraform apply /Users/cp/projects/work/infrastructure/src/aws/global/tfplan.out -input=false -auto-approve
Too many command line arguments. Configuration path expected.
[snip]

I run in to a lot of issues like being able to pass -terragrunt-source or -terragrunt-source-update flags (I need to use env) because the extra arguments are placed before any extra arguments given. I can see why that is since the feature is for flags, but I think this is still a valuable feature to have built in to terragrunt.

As far as the feature being "need way to...": I'm not sure that's the best solution either, but maybe some field called arguments_post or pre_arguments would solve this in a way that is clear to others.

brikis98 commented 6 years ago

Looks like this method is not behaving as intended: https://github.com/gruntwork-io/terragrunt/blob/master/options/options.go#L177. PRs with a fix are welcome.

seppi91 commented 5 years ago

I also came across this issue when trying to implement a plan-all command which persists a planfile, that is used by apply-all later. This would work perfectly fine if terragrunt wouldn't try to add -input=false -auto-approve at the end of the cmd.

[terragrunt] [/home/sebastian/Development/tg-boatyard/tg-timeseriesmonitor/aws/eu-central-1/base/rds] 2019/01/02 11:33:07 Detected 1 Hooks [terragrunt] [/home/sebastian/Development/tg-boatyard/tg-timeseriesmonitor/aws/eu-central-1/base/rds] 2019/01/02 11:33:07 Running command: terraform apply /home/sebastian/Development/tg-boatyard/tg-timeseriesmonitor/aws/eu-central-1/base/rds/tfplan.out -input=false -auto-approve Too many command line arguments. Configuration path expected.

these parameters should have to be ordered before the specific planfile. Resulting in the following cmd which would work when running manually

terraform apply -input=false -auto-approve /tfplan.out

tekollt commented 5 years ago

Is there any way around this? We implemented extra arguments in tfvars as described in #428 to save multiple plans as part of CI, but are now seeing the same thing.

lopezm1 commented 5 years ago

+1, just came across this. Will try to find some time to look into what's causing the error and fix. @brikis98

brikis98 commented 5 years ago

Great, thanks @lopezm1!

carlosfrancia commented 5 years ago

I had the same issue and I found a workaround/possible fix.

Why do we need -input=false -auto-approve by default?

I removed them from configstack/stack.go

// Apply all the modules in the given stack, making sure to apply the dependencies of each module in the stack in the
// proper order.
func (stack *Stack) Apply(terragruntOptions *options.TerragruntOptions) error {

    // stack.setTerraformCommand([]string{"apply", "-input=false", "-auto-approve"})

    stack.setTerraformCommand([]string{"apply"})
    return RunModules(stack.Modules)
}

Added those arguments to my terragrunt configuration (only if you require them):

  terraform {
    extra_arguments "plan_vars" {
      commands = [
        "plan",
      ]

      arguments = [
        "-out",
        "${get_tfvars_dir()}/plan.tfplan",
      ]
    }

    extra_arguments "apply_vars" {
      commands = [
        "apply",
      ]

      arguments = [
        "-auto-approve",
        "-input=false",
        "${get_tfvars_dir()}/plan.tfplan",
      ]
    }
  }

And the command is now valid and applies the plan:

Running command: terraform apply -auto-approve -input=false plan.tfplan

zot24 commented 5 years ago

@carlosfrancia just wondering if you have submit a PR with your fix?

zot24 commented 5 years ago

jtlyk I have created this PR https://github.com/gruntwork-io/terragrunt/pull/848 and instead of removing those options as @carlosfrancia mentioned I re organize them on the execution as @seppi91 and @brikis98 mentioned above and that solved my use case plz feedback :)