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

Terragrunt crashes with no useful information if terraform version is not determined #3422

Closed guhcampos closed 1 month ago

guhcampos commented 1 month ago

Describe the bug

Background

I work on a company that has recently been through M&A, so I have to manage two different environments, one of them uses tfswitch and the other tfenvto achieve essentially the same thing. The infrastructure using tfenv also uses terragrunt. Often I'll find myself in situations where tfenv and tfswitch interfere with each other. Recently, I have started to stumble in terragrunt crashes with no reasonable explanation.

Bug

After some troubleshooting I believe this is caused by terraform --version failing. On a non-debug session, terragrunt simply crashes:

❯ terragrunt plan
13:55:45.661 ERROR  [../some_project] exit status 1

Tracing the execution gives better insight:

❯ terragrunt --terragrunt-log-level trace plan
13:55:41.281 DEBUG  Terragrunt Version: 0.67.9
13:55:41.282 DEBUG  Found locals block: evaluating the expressions.
13:55:41.285 DEBUG  Found locals block: evaluating the expressions.
13:55:41.286 DEBUG  [../..] Found locals block: evaluating the expressions.
13:55:41.286 DEBUG  [../..] Found locals block: evaluating the expressions.
13:55:42.237 DEBUG  Evaluated 4 locals (remaining 6): env_vars, overrides, source_repo, secrets
13:55:42.237 DEBUG  Evaluated 6 locals (remaining 0): state_region, state_role_arn, state_encryption_kms_key_id, atlantis_skip, state_bucket, state_dynamodb_table
13:55:42.248 DEBUG  Found locals block: evaluating the expressions.
13:55:42.249 DEBUG  [../..] Found locals block: evaluating the expressions.
13:55:42.251 DEBUG  Evaluated 4 locals (remaining 6): env_vars, source_repo, overrides, secrets
13:55:42.251 DEBUG  Evaluated 6 locals (remaining 0): state_bucket, state_encryption_kms_key_id, state_region, state_role_arn, state_dynamodb_table, atlantis_skip
13:55:42.264 DEBUG  git show-toplevel result: 

../../../..

13:55:42.266 DEBUG  Exposing include block 'root'
13:55:42.272 DEBUG  Found locals block: evaluating the expressions.
13:55:42.272 DEBUG  [../..] Found locals block: evaluating the expressions.
13:55:42.273 DEBUG  Evaluated 4 locals (remaining 6): secrets, env_vars, overrides, source_repo
13:55:42.273 DEBUG  Evaluated 6 locals (remaining 0): atlantis_skip, state_encryption_kms_key_id, state_role_arn, state_dynamodb_table, state_region, state_bucket
13:55:42.275 DEBUG  [Partial] Included config ../../../terragrunt.hcl has strategy shallow merge: merging config in (shallow).
13:55:42.275 DEBUG  Running command: terraform --version
13:55:42.299 DEBUG  util.ProcessExecutionError [../rds_service_management] exit status 1
github.com/gruntwork-io/terragrunt/shell/run_shell_cmd.go:264 (0x103634eb9)
github.com/gruntwork-io/terragrunt/telemetry/metrics.go:42 (0x102fb5878)
github.com/gruntwork-io/terragrunt/telemetry/telemetry.go:80 (0x103633c74)
github.com/gruntwork-io/terragrunt/telemetry/traces.go:38 (0x102fb7a98)
github.com/gruntwork-io/terragrunt/telemetry/telemetry.go:79 (0x103633b88)
github.com/gruntwork-io/terragrunt/shell/run_shell_cmd.go:127 (0x1036338f1)
github.com/gruntwork-io/terragrunt/shell/run_shell_cmd.go:95 (0x1036337a8)
github.com/gruntwork-io/terragrunt/cli/commands/terraform/version_check.go:97 (0x103ae8d60)
github.com/gruntwork-io/terragrunt/cli/commands/terraform/version_check.go:55 (0x103ae8b08)
github.com/gruntwork-io/terragrunt/cli/commands/terraform/action.go:97 (0x103ae0c7c)
github.com/gruntwork-io/terragrunt/cli/commands/terraform/action.go:83 (0x103ae0b58)
github.com/gruntwork-io/terragrunt/cli/commands/terraform/command.go:47 (0x10411d4a0)
github.com/gruntwork-io/terragrunt/cli/app.go:239 (0x10411ee48)
golang.org/x/sync@v0.8.0/errgroup/errgroup.go:78 (0x1035fcb24)
runtime/asm_arm64.s:1223 (0x1029ab6e4)

13:55:42.299 ERROR  [../some_project] exit status 1

And finally:

❯ terraform --version
cat: /opt/homebrew/Cellar/tfenv/3.0.0/version: No such file or directory
Version could not be resolved (set by /opt/homebrew/Cellar/tfenv/3.0.0/version or tfenv use <version>)

Steps To Reproduce

I believe any situation where terraform --version errors out will trigger the issue.

Expected behavior

While naturally terragrunt can't work without a valid terraform binary, it would be useful to have the issue correctly reported instead of crashing. A useful error message would contain some relevante information about the output of terraform --version, in my case something like:

ERROR: failure to determine terraform version. Output from `terraform --version`:
> cat: /opt/homebrew/Cellar/tfenv/3.0.0/version: No such file or directory
> Version could not be resolved (set by /opt/homebrew/Cellar/tfenv/3.0.0/version or tfenv use <version>)
 [../some_project] exit status 1

Versions

terragrunt version 0.67.9

denis256 commented 1 month ago

Improved logging in https://github.com/gruntwork-io/terragrunt/releases/tag/v0.67.10