terraform - 如何在Terraform中获取调用模块的名称

标签 terraform

如何从子模块中访问父/调用模块的名称。
或者,如何从子模块中获取调用模块的文件名?

例如

module "test_parent_module" {
    source = "./child_module"
}
module "child_module" {
    locals {
        // What could I use here to get parent module name?
        parent_module_name = module.parent # Should output "test_parent_module"
        // What could I use here to get the parent module's file name?
        parent_file_name = module.parent.filename # Should output "test_parent_module.tf"
    }

}

谢谢

最佳答案

你不能。这种类型的继承层次结构不适用于 terraform。我建议改为传递变量。 (尽管如果您依赖 terraform 文件名,您的配置中似乎还存在其他问题,因此您可能需要重新检查您的代码结构)

module "test_A_module" {} // what you had as parent
module "test_B_module" {
    source = "./child_module"

    module_name = "test_A_module"
    file_name = "test_A_module.tf"
}
module "child_module" {
    locals {
        module_name = var.module_name
        file_name = var.file_name
    }

}

关于terraform - 如何在Terraform中获取调用模块的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65307590/

相关文章:

amazon-web-services - Terraform:启动源实例时出错:InvalidAMIID.Malformed

amazon-web-services - AWS ElastiCache 集群和 AWS ElastiCache 复制组有什么区别?

terraform - 变量值的 Heredoc 语法

terraform - 如何在Terraform中将列表转换为字符串?

地形。映射到模块中的 tfvars

yaml - 如何使用 Terraform 访问 yaml 文件中的 map

google-cloud-platform - 用于云存储传输的 BigQuery 数据传输服务是否可以使用 Terraform 实现?

terraform - 如何使用 Terraform 连接到 OCI?

kubernetes - Terraform Kubernetes 供应商 "local-exec"kubectl apply -f -<<EOF 在 Windows 上不起作用

terraform - 如何使用 Terraform 缓存下载的 ISO?