Terragrunt 无法从导入的源获取输出

问题描述 投票:0回答:6

我一直在关注 terragrunt 文档,最终得到了如下所示的结构:

  • 服务/磅
  • 服务/后端
  • 专有网络

我在 services/lb 和 services/backend 中设置了 vpc 的依赖关系,它可以独立工作,使 terragrunt run-all 适用。

然后我尝试将此配置移动到基础设施模块,如下https://terragrunt.gruntwork.io/docs/getting-started/quick-start/#promote-immutable-versioned-terraform-modules-across-environments并创建具有不同阶段的新存储库:

  • 产品
  • 舞台
  • ...

通过此操作,我了解到(如果我没有遗漏任何内容),在导入源时,该源的 terragrunt.hcl 依赖项配置将被忽略。

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  

因此,我在基础设施实时 terragrunt 模块中添加显式依赖项配置:

基础设施-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::[email protected]:project/infrastructure-modules.git//vpc"

}

基础设施-live/prod/backend/terragrunt.hcl

terraform {
  source = "git::[email protected]: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
}

来自 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: []) 

但现在它说依赖项没有输出。但它们是导入源模块中的输出!

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

这是 VPC 的 terragrunt 缓存。我们可以看到outputs.tf存在,所以vpc有输出。

输出内容.tf

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

我缺少什么?我必须做什么才能访问导入源的输出?

非常感谢您的帮助。

terraform cloud devops infrastructure terragrunt
6个回答
2
投票

此问题已经有一个开放的票证:

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

时隔两年,票证仍未解决。我用来让它工作的技巧是首先实际生成 terraform 模块的输出,然后使用 terragrunt 中的依赖项块。例如,如果您使用 Github Actions,则在运行 terragrunt plan 之前您需要执行以下操作:

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

然后在使用依赖块的 terragrunt.hcl 中:

dependency "dependency_settings" {
  config_path = "../dependency-settings"
}

1
投票

对于那些仍然受此问题困扰的人,您还可以尝试在依赖项中添加

mock_outputs
并看看它是否有效:

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


1
投票

我们在将资源导入 terragrunt 时遇到了同样的问题。 显然,当在依赖路径中运行

terragrunt state pull
时,我们发现“outputs”对象在状态下为空。这也可以通过运行
terragrunt output
来检查。 运行
terragrunt refresh
解决了它!


1
投票

我不能说导入模块时有用,但可以肯定地说,如果模块 B 依赖于模块 A 的输出并且未部署模块 A,则无法成功地为模块 B 进行“terragrunt 计划”。希望 terragunt v.1.0 能解决这个问题。


0
投票

感谢@stan666n,他的解决方案对我有用,这是我的解决方案。

locals {
  config_vars = read_terragrunt_config("../../../configurations/config.dev.hcl")
}

terraform {
  source = "../../../modules/serverless-vpc-access"
}

dependency "vpc" {
  config_path = "../cloud-vpc"

  mock_outputs = {
    private_network_name = "dummy-network-name"
    private_network_id   = "projects/${local.config_vars.locals.project}/global/networks/dummy-network-name"
  }
}

inputs = {

  # ════════════════════════  Clous SQL Inputs ══════════════════════════════

  private_network_name                 = "${dependency.vpc.outputs.private_network_name}"
  private_network_id                   = "${dependency.vpc.outputs.private_network_id}"
}

-1
投票

您应该使用以下依赖项:

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

您的 vpc 路径似乎错误,因此 terragrunt 未检测到任何输出。

© www.soinside.com 2019 - 2024. All rights reserved.