Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud platform 如何输出所创建资源的属性?_Google Cloud Platform_Terraform_Terraform Provider Gcp - Fatal编程技术网

Google cloud platform 如何输出所创建资源的属性?

Google cloud platform 如何输出所创建资源的属性?,google-cloud-platform,terraform,terraform-provider-gcp,Google Cloud Platform,Terraform,Terraform Provider Gcp,我正在执行一个GCP模块来创建一个服务帐户 main.tf: 创建帐户后,将创建一个terraform.tfstate文件,其中包含帐户的所有详细信息 terraform.tfstate 正如您在上面看到的,在模块中,我将输出account\u id输入变量。是否有方法输出属性即id,名称等,以便其他模块可以访问它们?属性是在创建资源后计算的。来自资源的文档: 将导出以下计算属性: 电子邮件-服务帐户的电子邮件地址。该值应从授予服务帐户权限的任何google_iam_策略数据源中引用 名称-服务

我正在执行一个GCP模块来创建一个服务帐户

main.tf: 创建帐户后,将创建一个
terraform.tfstate
文件,其中包含帐户的所有详细信息

terraform.tfstate 正如您在上面看到的,在模块中,我将输出
account\u id
输入变量。是否有方法输出
属性
即<代码>id,
名称
等,以便其他模块可以访问它们?
属性是在创建资源后计算的。

来自资源的文档:

将导出以下计算属性:

电子邮件
-服务帐户的电子邮件地址。该值应从授予服务帐户权限的任何google_iam_策略数据源中引用

名称
-服务帐户的完全限定名称

唯一\u id
-服务帐户的唯一id

您可以使用这些属性声明输出,方法与声明您的
account\u id
输出相同。例如:

   output "id" {
     value = "${google_service_account.gsvc_account.unique_id}"
   }

   output "email" {
     value = "${google_service_account.gsvc_account.email}"
   }

回复:“以便其他模块可以访问它们”。。。如果“其他模块”使用相同的状态文件,则上述输出可使用

  • ${google\u service\u account.gsvc\u account.account\u id}
  • ${google\u service\u account.gsvc\u account.email}
。。。i、 你根本不需要输出。因此,我猜测“其他模块”位于单独的项目/工作区/repo中,因此使用的是不同的状态文件。如果是,那么您将通过访问这些输出。例如,您可以声明远程状态数据源,以指向包含输出的任何状态:

resource "terraform_remote_state" "the_other_state" {
  backend = "..."
  config {
    ...
  }
}


然后参考该状态下的输出,如下所示:

  • ${terraform\u remote\u state.the\u other\u state.output.account\u id}
  • ${terraform\u remote\u state.the\u other\u state.output.email}

如果您的其他模块针对不同的状态文件运行(例如您的Terraform代码位于单独的目录中),那么您最好使用,而不是尝试将资源的值输出到您的状态文件并使用获取它们

google\u service\u账户
数据源的文档展示了您将如何使用它:

data "google_service_account" "myaccount" {
  account_id = "myaccount-id"
}

resource "google_service_account_key" "mykey" {
  service_account_id = "${data.google_service_account.myaccount.name}"
}

resource "kubernetes_secret" "google-application-credentials" {
  metadata = {
    name = "google-application-credentials"
  }
  data {
    credentials.json = "${base64decode(google_service_account_key.mykey.private_key)}"
  }
}
这避免了需要配置远程状态数据源,并且可以大大简化。事实上,在提供者有合适的数据源的任何情况下,我都建议以这种方式访问有关现有资源的信息。如果有另一种获取信息的方式(例如通过云提供商CLI),我甚至会推荐使用
terraform\u remote\u state
数据源,因为
terraform\u remote\u state
数据源特别笨重

resource "terraform_remote_state" "the_other_state" {
  backend = "..."
  config {
    ...
  }
}


data "google_service_account" "myaccount" {
  account_id = "myaccount-id"
}

resource "google_service_account_key" "mykey" {
  service_account_id = "${data.google_service_account.myaccount.name}"
}

resource "kubernetes_secret" "google-application-credentials" {
  metadata = {
    name = "google-application-credentials"
  }
  data {
    credentials.json = "${base64decode(google_service_account_key.mykey.private_key)}"
  }
}