Terragrunt中依赖模块无输出时如何通过run-all跳过当前模块?
解决方案:动态跳过依赖已销毁的模块
可行,你可以通过Terragrunt的try函数结合skip配置项,实现当依赖模块无输出时自动跳过当前模块,无需重复执行run-all destroy。
具体修改你的terragrunt.hcl如下:
dependency "workspace" { config_path = "../workspace" } locals { # 尝试读取依赖输出,若依赖模块已销毁则返回null workspace_resource_id = try(dependency.workspace.outputs.resource_id, null) # 当依赖输出为null时,标记当前模块需要跳过 skip_module = local.workspace_resource_id == null } # 动态设置是否跳过当前模块 skip = local.skip_module inputs = { databricks_resource_id = local.workspace_resource_id }
原理说明
try函数会捕获依赖输出不存在的错误,返回预设的null值,避免Terragrunt因依赖报错终止执行;- 通过
skip配置项,当local.skip_module为true时,Terragrunt在执行run-all命令时会自动跳过当前模块; - 执行
apply时,依赖模块存在输出,local.skip_module为false,模块正常执行,完全符合你的原有需求。
配合--terragrunt-ignore-dependency-errors参数使用时,首次run-all destroy会销毁依赖模块,后续执行时当前模块会因依赖输出不存在自动跳过,无需重复执行多次。
内容的提问来源于stack exchange,提问作者Chris Snow
相关产品推荐
相关产品推荐

