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
7.91k stars 966 forks source link

Regression: terragrunt-info does not work with mock outputs #3270

Open caspar-ds opened 1 month ago

caspar-ds commented 1 month ago

Describe the bug terragrunt-info fails to work with mock outputs of a dependency. This was fixed in #2885 and v0.56.1, however seems to have regressed at some point.

To Reproduce

Module A

# config/module-a/terragrunt.hcl
terraform {
  source = ".//"
}

# config/module-a/main.tf
output "test_var" {
  value = "hello"
} 

Module B

# config/module-b/terragrunt.hcl
terraform {
  source = ".//"
}

dependency "module_a" {
  config_path                             = "../module-a"
  mock_outputs_allowed_terraform_commands = ["init", "plan", "validate"]
  mock_outputs_merge_strategy_with_state  = "shallow"
  mock_outputs = {
    test_mock = "abc"
  }
}

inputs = {
  test_var = dependency.module_a.outputs.test_mock
}

# config/module-b/main.tf
variable "test_var" {
  type = string
}

output "result" {
  value = var.test_var
}

Working: cd config/module-b && terragrunt plan

INFO[0000] Downloading Terraform configurations from file:///my-home/terragrunt-info-test/config/module-a into /my-home/terraform.d/.terragrunt-cache/AnwNMwh9yg0XKSaMbmsxdNYr1BI/o12cAWD2nsVQ_xmslaEWdI5j6Fs  prefix=[/my-home/terragrunt-info-test/config/module-a] 

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.

Changes to Outputs:
  + result = "abc"

You can apply this plan to save these new output values to the Terraform
state, without changing any real infrastructure.

─────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.

Failing: cd config/module-b && terragrunt terragrunt-info

terragrunt terragrunt-info 

ERRO[0000] /my-home/terragrunt-info-test/config/module-a/terragrunt.hcl is a dependency of /my-home/Developer/oss/terragrunt-info-test/config/module-b/terragrunt.hcl but detected no outputs. Either the target module has not been applied yet, or the module has no outputs. If this is expected, set the skip_outputs flag to true on the dependency block. 
ERRO[0000] Unable to determine underlying exit code, so Terragrunt will exit with error code 1 

Expected behavior terragrunt-info should use the mock outputs when running

Versions

Additional context The same happens with an initialised and applied module where the output is new and has not yet been applied and saved to state. This will result in the outputs object not containing any mock outputs.

caspar-ds commented 1 month ago

For information, 0.56.1 would successfully print the info, but would still exit 1

╰─ terragrunt terragrunt-info                                                                                                                                                                          ─╯
ERRO[0000] Error: Unsupported attribute

ERRO[0000]   on /***/config/module-b/terragrunt.hcl line 15: 
ERRO[0000]   15:   test_var = dependency.module_a.outputs.test_mock 
ERRO[0000]                                              
ERRO[0000] This object does not have an attribute named "test_mock".

{
  "ConfigPath": "/***/config/module-b/terragrunt.hcl",
  "DownloadDir": "/***/config/module-b/.terragrunt-cache",
  "IamRole": "",
  "TerraformBinary": "terraform",
  "TerraformCommand": "terragrunt-info",
  "WorkingDir": "/***/config/module-b"
}
ERRO[0000] /***/config/module-b/terragrunt.hcl:15,41-51: Unsupported attribute; This object does not have an attribute named "test_mock". 
ERRO[0000] Unable to determine underlying exit code, so Terragrunt will exit with error code 1 
caspar-ds commented 1 month ago

Same thing happens with terragrunt show - seems there's a handful of commands that aren't parsing the mock outputs