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

ams*_*ams 4 terraform terraform-provider-gcp terraform-cloud

我的 terraform gcp provider 配置看起来像

provider "google" {
  project     = var.project
  region      = var.region
  credentials = file("account.json")
}
Run Code Online (Sandbox Code Playgroud)

我想在 terraform 云上运行我的 terraform 文件,但我不想将 account.json 文件放在源代码管理中。如何将 json GCP 服务帐户文件存储在 terraform cloud 中,然后从 terraform 脚本访问它?

Ala*_*Dea 5

您可以在 Terraform Cloud UI中将凭据作为名为google_credentials多行值提供,并将其标记为敏感值,然后为您的帐户输入正确的值(可能只是您的 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"
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以将工作区变量中的凭据提供给 Terraform 模块中的google提供程序,如下所示作为将被解释为 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
}
Run Code Online (Sandbox Code Playgroud)

凭据 -(可选)JSON 格式的服务帐户密钥文件的路径或内容。您可以使用 Cloud Console 管理密钥文件。

来自Google 提供程序配置参考