如何为 Terraform 配置凭证文件

How to configure credential file for Terraform

我在 terraform 中配置了以下提供程序:

provider "google" {
  credentials = "${file("key.json")}"
  project     = "project-123456"
  region      = "${var.region}"
}

我能够将项目名称移动到我在调用 terraform plan and apply 时传入的变量。但是凭证密钥文件似乎不可配置。

provider "google" {
  credentials = "${var.key}"
  project     = "${var.project}"
  region      = "${var.region}"
}

terraform plan -var key='${file("key.json")}' -var project=project-123456

引发此错误:

  • provider.google: credentials are not valid JSON '${file("key.json")}': invalid character '$' looking for beginning of value

我也这样试过:

provider "google" {
  credentials = "${file(${var.key})}"
  project     = "${var.project}"
  region      = "${var.region}"
}

terraform plan -var key=key.json -var project=project-123456

但是它抛出这个错误:

Error reading config for provider config google: parse error at 1:8: expected expression but found invalid sequence "$"

如何为提供商配置凭据文件?

我猜对了!在我最后一次尝试中只需要一些额外的引号:

credentials = "${file(${var.key})}"

credentials = "${file("${var.key}")}"

terraform plan -var key=key.json -var project=project-123456