teru_0x01.log

技術メモと雑記

GCP: googleapi: Error 400: Role roles/** is not supported for this resource., badRequest

terraformでサービスアカウント作って,IAMロールを付与しようとしたときにこうなった. 付与しようとしたのはroles/cloudsql.adminなど,ロールについてに掲載されているもの.

原因と解決策

次のようにgoogle_service_account_iam_policyを使っている(一番下のブロック)のが原因だった.

resource "google_service_account" "service_account" {
  project = var.project_id
  account_id   = "gketosql"
  display_name = "gketosql"
}

data "google_iam_policy" "sqladmin" {
  binding {
    role = "roles/cloudsql.admin"

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

resource "google_service_account_iam_policy" "cloudsql-binding" {
  service_account_id = google_service_account.service_account.name
  policy_data        = data.google_iam_policy.sqladmin.policy_data
}

正しくはgoogle_project_iam_bindingを使ってバインディングする.

resource "google_project_iam_binding" "cloudsql-binding" {
  project = var.project_id
  role    = "roles/cloudsql.client"
  members = [
    "serviceAccount:${google_service_account.service_account.email}",
  ]
}

思いっきりドキュメントの先頭に書いてある...

This resource is to add iam policy bindings to a service account resource to configure permissions for who can edit the service account. To configure permissions for a service account to act as an identity that can manage other GCP resources, use the google_project_iam set of resources.