我使用Terraform设置远程后端来管理状态。我已经为Terraform状态文件的连接和GCS声明设置了connection.tf文件。文件内容如下所示。
provider "google" {
credentials = "${file("../../secrets/account-thrashingcode.json")}"
project = "thrashingcorecode"
region = "us-west1"
}
terraform {
backend "gcs" {
bucket = "terraform-remote-states"
path = "dev/terraform.tfstate"
project = "thrashingcorecode"
}
}
我设置的资源,至少在这个例子中,是一个超级简单的配置,用于在GCP中创建默认网络。这种配置看起来像这样。
data "google_compute_network" "my-network" {
name = "default-us-west1"
}
现在,当我运行terraform init时,我收到此错误。
$ terraform init
Initializing the backend...
Successfully configured the backend "gcs"! Terraform will automatically
use this backend unless the backend configuration changes.
Error refreshing state: [WARN] Error retrieving object blue-world-terraform-state/dev/terraform.tfstate: googleapi: got HTTP response code 403 with body: adronsotheremail@gmail.com does not have storage.objects.get access to blue-world-terraform-state/dev/terraform.tfstate.
This leaves me with a few questions.
Terraform将“adronsotheremail@gmail.com”电子邮件标识作为尝试访问存储位置的帐户派生到哪里?它似乎与我创建资源的帐户实际上不是相同的电子邮件。 如果我可以使用在阶段1中拥有所有权的服务帐户创建资源,那么在这种特定情况下它用于权限的是什么?它确实存在如图所示:
我首次尝试修复此问题是转到存储资源并添加此帐户以确保其拥有此资源的权限。
这解决了问题,但我仍然不完全确定为什么我必须添加该成员。不应该创建Terraform,理论上我认为我正在使用连接中connection.tf文件中详述的连接信息吗?
参考:我已经详细阐述了整个过程,以及我在here工作的多个阶段。
答案 0 :(得分:1)
关键问题是:
” Terraform在哪里派生“ adronsotheremail@gmail.com”电子邮件 身份作为尝试访问存储位置的帐户?”
答案是它从应用程序默认凭据获取了该帐户。要切换到其他默认凭据,请运行:
gcloud auth应用程序-默认登录
另请参阅:https://cloud.google.com/docs/authentication/production
答案 1 :(得分:0)
好的,就像命运一样,我想出来了。需要设置的是ACL,这并不是特别由创建存储资源的因素决定的。我最终解决的问题是在我的Terraform配置中设置资源,我将ACL设置为 predefined_acl 属性设置之一。更多内容google docs on ACLs。
resource "google_storage_bucket_acl" "image-store-acl" {
bucket = "${google_storage_bucket.blue-world-tf-state.name}"
predefined_acl = "publicreadwrite"
}