有没有办法获取 terraform 提供者的属性并将其存储为局部变量?

Is there a way to get terraform provider's attribute and store it as a local variable?

我在最顶层模块中定义了一个提供程序,有没有办法在子模块中访问它的属性?

一个例子:

最顶层模块:

provider "google" {
    project = "foo"
}
module "child" {
    source = "./child"
}

子模块:

locals {
    foo = google.project // this won't work (probably because it clashes with alias access syntax)
}

遗憾的是,您不能像这样引用提供者。对此的要求已经很少,例如here or here.

相反,在您的情况下,您可以使用数据源:

data "google_project" "project" {}

locals {
    foo = data.google_project.project.project_id
}

或者将项目id设为变量:

variable "project_id" {
   default = "foo"
}

provider "google" {
    project =  var.project_id
}

locals {
    foo = var.project_id
}