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.82k stars 962 forks source link

Dependency with workspace #2419

Open SamuelMolling opened 1 year ago

SamuelMolling commented 1 year ago

Basically I have a module that depends on another module, but I use workspaces. When I run an init in gitactions it gives me the following error:

time=2023-01-16T11:57:58Z level=error msg=xx/module1/terragrunt.hcl is a dependency of xx/module2/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.
time=2023-01-16T11:57:58Z level=error msg=Unable to determine underlying exit code, so Terragrunt will exit with error code 1

I searched in several forums and I couldn't solve it, because:

1 - When I run a run-all init, it says that the default workspace has no output. It's true because it only exists in the develop workspace

yes | terragrunt run-all init
yes | terragrunt workspace select develop
Module MODULE2 depends on module MODULE2, which is an external dependency outside of the current working directory. Should Terragrunt run this external dependency? Warning, if you say 'yes', Terragrunt will make changes in MODULE2 as well! (y/n) time=2023-01-13T15:35:50Z level=info msg=The stack at MODULE1 will be processed in the following order for command init:
Group 1
- Module MODULE2

Group 2
- Module MODULE2

Initializing the backend...

Successfully configured the backend "gcs"! Terraform will automatically
use this backend unless the backend configuration changes.

Initializing provider plugins...
- Finding latest version of mongodb/mongodbatlas...
- Finding latest version of hashicorp/google...
- Installing mongodb/mongodbatlas v1.6.1...
- Installed mongodb/mongodbatlas v1.6.1 (signed by a HashiCorp partner, key ID 2A32ED1F3AD25ABF)
- Installing hashicorp/google v4.48.0...
- Installed hashicorp/google v4.48.0 (signed by HashiCorp)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

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.
time=2023-01-13T15:35:59Z level=error msg=Module MODULE2 has finished with an error: MODULE2/terragrunt.hcl is a dependency of MODULE2/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. prefix=[MODULE2] 
time=2023-01-13T15:35:59Z level=error msg=1 error occurred:
    * MODULE2/terragrunt.hcl is a dependency of MODULE2/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.

time=2023-01-13T15:35:59Z level=error msg=Unable to determine underlying exit code, so Terragrunt will exit with error code 1
yes: standard output: Broken pipe

2 - When I try to switch the workspace first, I also get an error.

The problem is solved when I upload the file of module 1 (putting a space in the file) and module 2 in the same plan, then it finds the output. I tried to refresh first, but it gives an error that there is no resource in the default workspace.

SamuelMolling commented 1 year ago

@denis256 you can help me ?

denis256 commented 1 year ago

Hi, was attempted to define some dummy values in the mock_outputs of the dependency?

https://terragrunt.gruntwork.io/docs/reference/config-blocks-and-attributes/#dependency

SamuelMolling commented 1 year ago

Yes, but it does not recover the dependency state and this causes problems in apply, causing it to keep dummy values.

I work with workspaces, as I mentioned earlier. But to retrieve the values of this dependency in my github action, I had to send a space in module 1's file for module 2 to retrieve the value. In this case, every change made by PR I was obliged to send an "alteration" in module 1

FlorentPoinsaut commented 8 months ago

A workaround is to use a generated block to set a terraform block like this:

generate "terraform" {
  path      = "terraform.tg.tf"
  if_exists = "overwrite_terragrunt"
  contents = <<EOF
terraform {
  cloud {
    organization = "my-org"
    workspaces {
      name = "${workspace}"
    }
  }
}
EOF
}