3

I've been following terragrunt docs and I end up with a structure like the following one:

  • services/lb
  • services/backend
  • vpc

I had set a dependency of vpc in services/lb and in services/backend and it works standalone making terragrunt run-all apply.

Then I tried to move this configuration to infrastructure modules following https://terragrunt.gruntwork.io/docs/getting-started/quick-start/#promote-immutable-versioned-terraform-modules-across-environments and create a new repository with the different stages:

  • prod
  • stage
  • ...

Making this I learned (if I am not missing anything) that when importing a source, terragrunt.hcl dependencies configuration of that source are ignored.

samuel@angel:~/Documents/infrastructure-live/prod$ terragrunt_linux_amd64 run-all apply                                                            
INFO[0000] Stack at /home/samuel/Documents/infrastructure-live/prod:                                                                               
  => Module /home/samuel/Documents/infrastructure-live/prod (excluded: false, dependencies: [])                                                    
  => Module /home/samuel/Documents/infrastructure-live/prod/services/backend (excluded: false, dependencies: [])                                   
  => Module /home/samuel/Documents/infrastructure-live/prod/services/lb (excluded: false, dependencies: [])                                        
  => Module /home/samuel/Documents/infrastructure-live/prod/vpc (excluded: false, dependencies: [])                                                
Are you sure you want to run 'terragrunt apply' in each folder of the stack described above? (y/n) y  

So I add explicit dependency configuration in my infrastructure-live terragrunt modules:

infrastructure-live/prod/vpc/terragrunt.hcl

include {
  path = find_in_parent_folders()
}
terraform {
  extra_arguments "common_vars" {
    commands = get_terraform_commands_that_need_vars()

    arguments = [
      "-var-file=../network.tfvars",
      "-var-file=../region.tfvars"
    ]
  }

  
  source = "git::git@gitlab.com:project/infrastructure-modules.git//vpc"

}

infrastructure-live/prod/backend/terragrunt.hcl

terraform {
  source = "git::git@gitlab.com:project/infrastructure-modules.git//services/backend"
}

include {
  path = find_in_parent_folders()
}

dependency "vpc" {
  config_path = "../../vpc"
}

inputs = {
  backend_vpc_id = dependency.vpc.outputs.backend_vpc_id
}

Dependency information from shell:

INFO[0000] Stack at /home/samuel/Documents/infrastructure-live/prod:
  => Module /home/samuel/Documents/infrastructure-live/prod (excluded: false, dependencies: [])
  => Module /home/samuel/Documents/infrastructure-live/prod/services/backend (excluded: false, dependencies: [/home/samuel/Documents/infrastructure-live/prod/vpc])
  => Module /home/samuel/Documents/infrastructure-live/prod/services/lb (excluded: false, dependencies: [/home/samuel/Documents/infrastructure-live/prod/vpc, /home/samuel/Documents/infrastructure-live/prod/services/backend])
  => Module /home/samuel/Documents/infrastructure-live/prod/vpc (excluded: false, dependencies: []) 

But now it says that the dependency does not have outputs. But they are outputs in the module of the imported source!

ERRO[0061] Module /home/samuel/Documents/infrastructure-live/prod/services/backend has finished with an error: /home/samuel/Documents/infrastructure-live/prod/vpc/terragrunt.hcl is a dependency of /home/samuel/Documents/infrastructure-live/prod/services/backend/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=[/home/samuel/Documents/infrastructure-live/prod/services/backend]ERRO[0061] Dependency /home/samuel/Documents/infrastructure-live/prod/services/backend of module /home/samuel/Documents/infrastructure-live/prod/services/lb just finished with an error. Module /home/samuel/Documents/infrastructure-live/prod/services/lb will have to return an error too.  prefix=[/home/samuel/Documents/infrastructure-live/prod/services/lb]                                                                                               
ERRO[0061] Module /home/samuel/Documents/infrastructure-live/prod/services/lb has finished with an error: Cannot process module Module /home/samuel/Documents/infrastructure-live/prod/services/lb (excluded: false, dependencies: [/home/samuel/Documents/infrastructure-live/prod/vpc, /home/samuel/Documents/infrastructure-live/prod/services/backend]) because one of its dependencies, Module /home/samuel/Documents/infrastructure-live/prod/services/backend (excluded: false, dependencies: [/home/samuel/Documents/infrastructure-live/prod/vpc]), finished with an error: /home/samuel/Documents/infrastructure-live/prod/vpc/terragrunt.hcl is a dependency of /home/samuel/Documents/infrastructure-live/prod/services/backend/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=[/home/samuel/Documents/infrastructure-live/prod/services/lb]ERRO[0061] Encountered the following errors:Cannot process module Module /home/samuel/Documents/infrastructure-live/prod/services/lb (excluded: false, dependencies: [/home/samuel/Documents/infrastructure-live/prod/vpc, /home/samuel/Documents/infrastructure-live/prod/services/backend]) because one of its dependencies, Module /home/samuel/Documents/infrastructure-live/prod/services/backend (excluded: false, dependencies: [/home/samuel/Documents/infrastructure-live/prod/vpc]), finished with anerror: /home/samuel/Documents/infrastructure-live/prod/vpc/terragrunt.hcl is a dependency of /home/samuel/Documents/infrastructure-live/prod/services/backend/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.                                                                                             
/home/samuel/Documents/infrastructure-live/prod/vpc/terragrunt.hcl is a dependency of /home/samuel/Documents/infrastructure-live/prod/services/backend/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[0061] Unable to determine underlying exit code, so Terragrunt will exit with error code 1

This is terragrunt cache for VPC. We can see that outputs.tf exists so vpc has outputs.

Terragrunt cache

Content of outputs.tf

output "backend_vpc_id" {
    value = digitalocean_vpc.backend_vpc.id
    description = "Backend VPC ID"
}

What I am missing? What I have to do to access to the outputs of a imported source?

Thanks a lot for the help.

Géminis
  • 123
  • 2
  • 9

5 Answers5

1

We experienced the same issue when importing resources into terragrunt. Apparently, when running terragrunt state pull in the dependency path, we found that the "outputs" object was empty in the state. This can also be checked by running terragrunt output. Running terragrunt refresh solves it!

shbedev
  • 1,875
  • 17
  • 28
1

There is already an open ticket for this issue:

https://github.com/gruntwork-io/terragrunt/issues/1330

The ticket has still not been resolved after two years. The hack I use to get it to work is to actually generate the outputs for the terraform module first and then use the dependency block in terragrunt. For example, if you were using Github Actions, you would do something like this before running terragrunt plan:

  - name: Check Outputs
    run: |
      cd modules/dependency-settings/infrastructure
      terraform init
      terraform refresh
      terraform output

And then in your terragrunt.hcl which uses the dependency block:

dependency "dependency_settings" {
  config_path = "../dependency-settings"
}
Daniel Viglione
  • 8,014
  • 9
  • 67
  • 101
0

For those who are still suffering about this issue, you may also try adding a mock_outputs to the dependency and see if it works:

dependency "vpc" {
  config_path = find_in_parent_folders("vpc")

  mock_outputs = {
    vpc_id      = "temp-id"
  }
}

inputs = {
  vpc_id = dependency.vpc.outputs.vpc_id
}

https://terragrunt.gruntwork.io/docs/features/execute-terraform-commands-on-multiple-modules-at-once/#unapplied-dependency-and-mock-outputs

Stan666
  • 424
  • 2
  • 5
  • 12
0

i can not say smth usefull when modules are imported but definetly can say that you cant "terragrunt plan" successfully for module B if it depends of outputs of module A and module A is not deployed. hope terragunt v.1.0 will solve it.

Alex
  • 837
  • 8
  • 9
-1

You should use the below dependency:

dependency "vpc" {
  config_path = find_in_parent_folders("vpc")
}

It looks like your vpc's path was wrong, so terragrunt detected no outputs.