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.09k stars 981 forks source link

duplicate error log output #3470

Closed grimm26 closed 1 month ago

grimm26 commented 1 month ago

Describe the bug

When there is a terraform error, the output is shown multiple times.

Steps To Reproduce

# something simple that will generate an error
locals {
  foo = ["bar"]
}

output "foo" {
  value = local.foo[2]
}

terragrunt plan that (with a bare minimum terragrunt.hcl in place).

Expected behavior

show error once.

example(s)

❯ command terragrunt version
Terraform v1.9.5
on linux_amd64
❯ command terragrunt -version
terragrunt version v0.68.1
❯ command terragrunt plan
Initializing the backend...
Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
╷
│ Error: Invalid index
│ 
│   on main.tf line 6, in output "foo":
│    6:   value = local.foo[2]
│     ├────────────────
│     │ local.foo is tuple with 1 element
│ 
│ The given key does not identify an element in this collection value.
╵
15:33:48.309 ERROR  terraform invocation failed in .
15:33:48.309 ERROR  error occurred:

* Failed to execute "terraform plan" in .
  ╷
  │ Error: Invalid index
  │ 
  │   on main.tf line 6, in output "foo":
  │    6:   value = local.foo[2]
  │     ├────────────────
  │     │ local.foo is tuple with 1 element
  │ 
  │ The given key does not identify an element in this collection value.
  ╵

  exit status 1
❯ env | grep TERRAGRUNT
TERRAGRUNT_TFPATH=terraform
TERRAGRUNT_FORWARD_TF_STDOUT=1
TERRAGRUNT_DISABLE_COMMAND_VALIDATION=1
❯ unset TERRAGRUNT_FORWARD_TF_STDOUT
❯ command terragrunt plan
15:37:42.191 STDOUT terraform: Initializing the backend...
15:37:42.191 STDOUT terraform: Initializing provider plugins...
15:37:42.191 STDOUT terraform: Terraform has been successfully initialized!
15:37:42.191 STDOUT terraform: 
15:37:42.191 STDOUT terraform: You may now begin working with Terraform. Try running "terraform plan" to see
15:37:42.191 STDOUT terraform: any changes that are required for your infrastructure. All Terraform commands
15:37:42.191 STDOUT terraform: should now work.
15:37:42.191 STDOUT terraform: If you ever set or change modules or backend configuration for Terraform,
15:37:42.191 STDOUT terraform: rerun this command to reinitialize your working directory. If you forget, other
15:37:42.191 STDOUT terraform: commands will detect it and remind you to do so if necessary.
15:37:42.236 STDERR terraform: ╷
15:37:42.236 STDERR terraform: │ Error: Invalid index
15:37:42.236 STDERR terraform: │ 
15:37:42.236 STDERR terraform: │   on main.tf line 6, in output "foo":
15:37:42.236 STDERR terraform: │    6:   value = local.foo[2]
15:37:42.236 STDERR terraform: │     ├────────────────
15:37:42.236 STDERR terraform: │     │ local.foo is tuple with 1 element
15:37:42.236 STDERR terraform: │ 
15:37:42.236 STDERR terraform: │ The given key does not identify an element in this collection value.
15:37:42.236 STDERR terraform: ╵
15:37:42.240 ERROR  terraform invocation failed in .
15:37:42.240 ERROR  error occurred:

* Failed to execute "terraform plan" in .
  ╷
  │ Error: Invalid index
  │ 
  │   on main.tf line 6, in output "foo":
  │    6:   value = local.foo[2]
  │     ├────────────────
  │     │ local.foo is tuple with 1 element
  │ 
  │ The given key does not identify an element in this collection value.
  ╵

  exit status 1

Versions

yhakbar commented 1 month ago

This is expected behavior. The idea here is that regardless of the number of errors that are encountered during a run-all, you will not have to search through a potentially long error log to uncover the root cause of your Terragrunt run. In the future, Terragrunt will also do more to suggest fixes based on what it detects as the underlying error in the stderr from OpenTofu/Terraform, if there's a trivial fix to recommend.

If you would like a feature to opt-out of this functionality via a flag like --terragrunt-no-error-summary or something, please feel free to document that request in a new issue.

The name I'm suggesting here is derived from the rules found under "Technical Details" --> "Document Design Philosophy" here: #3445. If you have any more feedback there as well, I'd greatly appreciate it, given your great feedback on making Terragrunt usage a great experience.