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

Ani*_*ket 7 google-cloud-storage google-cloud-platform terraform terraform-template-file terraform-provider-gcp

我创建了一个 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"
}
Run Code Online (Sandbox Code Playgroud)

错误:已附在此输入图像描述

如果有人可以帮助我在创建存储桶时分配权限,那就太好了。

Nib*_*s H 5

根据Terraform 官方文档,使用该函数bucket.name,并从变量中读取存储桶名称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"
}
Run Code Online (Sandbox Code Playgroud)

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


Ani*_*ket 5

以下设置解决了我的问题:

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"
}
Run Code Online (Sandbox Code Playgroud)