terraform - 如何将 GCP 服务帐户 JSON 存储在 terraform 变量中?

标签 terraform terraform-provider-gcp terraform-cloud

我的 terraform gcp 提供程序配置看起来像

provider "google" {
  project     = var.project
  region      = var.region
  credentials = file("account.json")
}
我想在 terraform cloud 上运行我的 terraform 文件,我不想将 account.json 文件放在源代码管理中。如何将 json GCP 服务帐户文件存储在 terraform 云中,然后从 terraform 脚本访问它?

最佳答案

您可以将凭据提供为 Multi-Line value调用 google_credentials 在 Terraform Cloud UI 中并将其标记为 Sensitive Value并为您的帐户输入正确的值(可能只是您已经拥有的 account.json 文件的复制粘贴):

{
  "type": "service_account",
  "project_id": "project-id",
  "private_key_id": "key-id",
  "private_key": "-----BEGIN PRIVATE KEY-----\nprivate-key\n-----END PRIVATE KEY-----\n",
  "client_email": "service-account-email",
  "client_id": "client-id",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/service-account-email"
}
然后,您可以将工作空间变量中的凭据提供给您的。谷歌 将 Terraform 模块中的 provider 作为单个变量,将其解释为 JSON:
provider "google" {
  project     = var.project
  region      = var.region
  credentials = var.google_credentials
}

variable "google_credentials" {
  description = "the contents of a service account key file in JSON format."
  type = string
}

credentials - (Optional) Either the path to or the contents of a service account key file in JSON format. You can manage key files using the Cloud Console.


来自 Google Provider Configuration Reference .

关于terraform - 如何将 GCP 服务帐户 JSON 存储在 terraform 变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62491106/

相关文章:

Azure 专用端点和 Terraform

azure - Terraform - 用于资源创建的嵌套循环

macos - 如何在Macbook上安装Terragrunt 0.19.x版本

google-cloud-platform - Google Cloud - 如何按标签或名称授予对 secret 组的访问权限?

google-cloud-platform - 使用 terraform 将 cloud-init 脚本发送到 gcp

terraform - 推送新镜像时的云运行部署模式,如果通过 terraform 创建服务,是否可以避免?

azure - Terraform 云使用 Azure 运行触发器

terraform - 如何使用 Terraform Cloud 作为远程后端发送本地文件?

Terraform 不接受变量默认插值或处理分层插值

terraform - 需要权限 iam.serviceAccounts.setIamPolicy 才能对服务帐户执行此操作