想要使用Terraform在GCP中部署具有公共可读存储对象权限的存储桶

时间:2020-02-04 10:12:39

标签: google-cloud-platform google-cloud-storage terraform terraform-provider-gcp terraform-template-file

我创建了一个terraform文件来创建具有公共可读存储对象许可权的Google存储桶。我能够部署存储桶,但无法为模板分配适当的ACL,但我发现ACL部件存在一些错误。

provider "google-beta" {
  project = "${var.project}"
}

 resource "google_storage_default_object_access_control" "public_rule" {
  bucket = "google_storage_bucket.test-${var.project}"
  role   = "READER"
  entity = "allUsers"
 }

resource "google_storage_bucket" "bucket" {
  name = "test-${var.project}"
  storage_class = "standard"
  location = "US"
}

错误:附加了enter image description here

如果在创建存储分区时有人可以帮助我分配权限,那将非常有用。

2 个答案:

答案 0 :(得分:1)

根据Terraform Official Documentation,使用函数“ bucket.name”,它从变量“名称”中读取存储桶名称。另外,您还必须在resource_storage_bucket中提供您的项目ID,如下所示。我尝试了一下,对我来说正常工作:

provider "google-beta" {
}
 resource "google_storage_default_object_access_control" "public_rule" {
  bucket = google_storage_bucket.bucket.name
  role   = "READER"
  entity = "allUsers"
 }
resource "google_storage_bucket" "bucket" {
  name = "[THE_BUCKET_NAME]"
  project = "[PROJECT_ID]"
  storage_class = "standard"
  location = "US"

其中PROJECT_ID是您的项目ID,THE_BUCKET_NAME是您要放置的存储桶名称。

答案 1 :(得分:1)

感谢尼布拉斯。

我要使用以下格式进行部署。它将解决我的问题。

  provider "google-beta" {
  project = "${var.project}"
}

data "google_iam_policy" "viewer" {
  binding {
    role = "roles/storage.objectViewer"
    members = [
        "allUsers",
    ] 
  }
}

resource "google_storage_bucket_iam_policy" "editor" {
  bucket = "${google_storage_bucket.bucket.name}"
  policy_data = "${data.google_iam_policy.viewer.policy_data}"
}

resource "google_storage_bucket" "bucket" {
  name = "${var.project}-xxxx"
  storage_class = "xxxxx"
  location = "xxxxxxx"
}