Terraform模块变量调用报错求助:主文件无法获取模块变量
报错原因及解决方法
- Terraform模块里定义在
variables.tf的输入变量属于模块内部入参,默认不会暴露给父模块直接调用。你尝试的module.network.iam_profile和module.network.var.iam_profile两种写法都不符合Terraform的模块引用规则,所以必然报错。 - 要在主TF文件中获取这个
iam_profile的值,必须在modules/network目录下创建outputs.tf文件,将该变量声明为模块输出,示例代码如下:
output "iam_profile" { value = var.iam_profile description = "The IAM profile configured in the network module" }
- 定义输出后,就能在主TF文件里通过
module.network.iam_profile正常引用该值,而module.network.var.iam_profile这种写法本身就是错误的,无论操作多少次terraform init或apply都无法生效。
内容的提问来源于stack exchange,提问作者NitroDeath666x
相关产品推荐
相关产品推荐

