如何在 Terraform 中将 `roles/bigquery.jobUser` 绑定到 GCP 项目?

huangapple go评论88阅读模式
英文:

How do I bind `roles/bigquery.jobUser` to a GCP project in terraform?

问题

我正在尝试在BigQuery中运行一个查询,但是出现了以下错误:

google.api_core.exceptions.Forbidden: 403 POST https://bigquery.googleapis.com/bigquery/v2/projects/my-project/jobs?prettyPrint=false: Access Denied: Project my-project: User does not have bigquery.jobs.create permission in project my-project.

因此,我需要在my-project中给我的服务账号赋予BigQuery Job User角色。

最初,我以为我需要将其绑定到数据集,所以写了以下内容,但实际上绑定需要在项目级别进行。然而,在Google提供程序的terraform文档中,我没有找到类似于将事物绑定到项目的模式。

以下是当我以为它绑定到数据集时的代码示例:

resource "google_bigquery_dataset_iam_binding" "dataset_job_user" {
  dataset_id = google_bigquery_dataset.dataset.dataset_id
  role       = "roles/bigquery.user"
  members    = [
    "serviceAccount:${google_service_account.my_service_account.email}"
  ]
}

请问如何在terraform中将此角色绑定到项目级别?

英文:

I'm trying to run a query in BigQuery, and am getting:

google.api_core.exceptions.Forbidden: 403 POST https://bigquery.googleapis.com/bigquery/v2/projects/my-project/jobs?prettyPrint=false: Access Denied: Project my-project: User does not have bigquery.jobs.create permission in project my-project.

So, I need to give my service account the BigQuery Job User role in my-project.

Initially, I thought that I would bind it to the dataset so wrote the following, but the binding needs to go to the project. However, in the terraform docs for the google provider I can't see a similar pattern for binding things to projects.

Here's what I had when I thought it was bound to the dataset:

resource "google_bigquery_dataset_iam_binding" "dataset_job_user" {
  dataset_id = google_bigquery_dataset.dataset.dataset_id
  role   = "roles/bigquery.user"
  members = [
    "serviceAccount:${google_service_account.my_service_account.email}"
  ]
}

How can I bind this role at the project level in terraform?

答案1

得分: 1

因为在文档的侧边栏中没有顶级资源,所以我忽略了 terraform 提供程序中的项目资源。这个资源对应 google_project_iam_binding,需要绑定到这个资源上。

正确的 terraform 代码如下:

resource "google_project_iam_member" "project_bigquery_job_user" {
  project = "${var.project}"
  role    = "roles/bigquery.jobUser"
  members = [
    "serviceAccount:${google_service_account.my_service_account.email}"
  ]
}
英文:

Because there's no top-level resource inteh sidebar of the docs, I had overlooked the project resource in the terraform provider. This has a corresponding google_project_iam_binding and needed to bind to this instead.

The correct terraform code is:

resource "google_project_iam_member" "project_bigquery_job_user" {
  project = "${var.project}"
  role    = "roles/bigquery.jobUser"
  members = [
    "serviceAccount:${google_service_account.my_service_account.email}"
  ]
}

huangapple
  • 本文由 发表于 2023年8月9日 17:13:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定