模块内的引用变量

Mah*_*ahi 5 terraform

我试图引用模块内声明的变量来更新同一模块中的另一个变量,但我无法找到有关如何引用该变量的指南。

\n

这是我的代码片段

\n
module "cluster" {\n  source = "..."\n\n  var1 = value1    # directly passing value\n  var2 = module.cluster.var1 # I need to update this variable value based on value of var1\n
Run Code Online (Sandbox Code Playgroud)\n

我在地形计划期间遇到以下错误

\n
module "cluster" {\n  source = "..."\n\n  var1 = value1    # directly passing value\n  var2 = module.cluster.var1 # I need to update this variable value based on value of var1\n
Run Code Online (Sandbox Code Playgroud)\n

我还尝试使用 local.var1 进行引用,如下所示

\n
module "cluster" {\n  source = "..."\n\n  var1 = value1    # directly passing value\n  var2 = local.var1 # I need to update this variable value based on value of var1\n
Run Code Online (Sandbox Code Playgroud)\n

然后我遇到以下错误

\n
Terraform v1.0.11\non linux_amd64\nConfiguring remote state backend...\nInitializing Terraform configuration...\n\n Error: Unsupported attribute\n\xe2\x94\x82 \n\xe2\x94\x82   on main.tf line 04, in module "cluster":\n\xe2\x94\x82   04:       var2 = module.cluster.var1\n\xe2\x94\x82     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n\xe2\x94\x82     \xe2\x94\x82 module.cluster is a object, known only after apply\n\xe2\x94\x82 \n\xe2\x94\x82 This object does not have an attribute named "var1".\n\n
Run Code Online (Sandbox Code Playgroud)\n

任何线索都会有帮助。

\n

问候

\n

Mar*_*k B 5

您的第二次尝试使用局部变量,是在正确的轨道上,但您必须实际声明局部变量

locals {
   var1 = value1
}

module "cluster" {
  source = "..."

  var1 = local.var1
  var2 = local.var1
Run Code Online (Sandbox Code Playgroud)