使用Terraform创建具有IAM角色的服务帐户

时间:2019-04-18 00:21:00

标签: terraform terraform-provider-gcp

我正在尝试使用roles/logging.logWriter IAM角色和Terraform创建一个基本服务帐户。以下是我的配置方式:

resource "google_service_account" "log_user" {
  account_id   = "log-user"
  display_name = "Logging User"
}

data "google_iam_policy" "log_policy" {
  binding {
    role = "roles/logging.logWriter"

    members = [
      "serviceAccount:${google_service_account.log_user.email}"
    ]
  }
}

resource "google_service_account_iam_policy" "log_user_policy" {
  service_account_id = "${google_service_account.log_user.name}"
  policy_data        = "${data.google_iam_policy.log_policy.policy_data}"
}

运行terraform apply时,我收到以下错误消息:

* module.iam.google_service_account_iam_policy.log_user_policy: 1 error(s) occurred:

* google_service_account_iam_policy.log_user_policy: Error setting IAM policy for service account 'projects/arcadia-apps-237918/serviceAccounts/log-user@arcadia-apps-237918.iam.gserviceaccount.com': googleapi: Error 400: Role roles/logging.logWriter is not supported for this resource., badRequest

从我的挖掘工作中,我似乎找不到关于如何创建服务帐户然后附加角色的明确解释。

1 个答案:

答案 0 :(得分:0)

我通过以下操作解决了这个问题:

resource "google_service_account" "log_user" {
  account_id   = "log-user"
  display_name = "Logging User"
}

resource "google_project_iam_binding" "log_user" {
  project = "arcadia-apps-237918"
  role    = "roles/logging.logWriter"
  members = [
    "serviceAccount:${google_service_account.log_user.email}"
  ]
}

运行terraform的用户必须先分配有IAM管理员角色,然后才能执行此操作。