LoginSignup
1
0

More than 1 year has passed since last update.

TerraformでBQデータセット作成時のアクセス権指定でのハマり("OWNER" -> nullという変更差分がplanで出続ける問題)

Last updated at Posted at 2022-11-05

TerraformでBQデータセットを作った際に、applyは成功するのですが、その直後planをしても変更差分を永遠検出し続けてしまうというハマりがあったので備忘録しておきます。

発生した問題と再現手順

以下のようにデータセットを作成し、アクセス件として、「roles/bigquery.dataOwner」を同じくTerraformで作ったサービスアカウントへ付与する。
ポイント:アクセス権を、google_bigquery_datasetaccess項で指定している

resource "google_bigquery_dataset" "transfer_dest" {
  dataset_id  = "transfer_dest"
  location    = "US"

  access {
    role          = "roles/bigquery.dataOwner"
    user_by_email = google_service_account.bq_upload_account.email
  }
}

resource "google_service_account" "bq_upload_account" {
  account_id = "bq_upload_account"
  description = "xxxxxxxxxxxxxx"
}

planで見ると、以下の通り。特に違和感は無し。

Terraform will perform the following actions:

  # module.stg.google_bigquery_dataset.transfer_dest will be created
  + resource "google_bigquery_dataset" "transfer_dest" {
      + creation_time              = (known after apply)
      + dataset_id                 = "dataset-id"
      + delete_contents_on_destroy = false
      + etag                       = (known after apply)
      + id                         = (known after apply)
      + last_modified_time         = (known after apply)
      + location                   = "US"
      + project                    = (known after apply)
      + self_link                  = (known after apply)

      + access {
          + role          = "roles/bigquery.dataOwner"
          + user_by_email = "bq_upload_account@project-name.iam.gserviceaccount.com"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

applyしてデータセット作成に成功した直後にplanをしたところ、以下のような差分を検知!
(これが当記事で言及している問題!)

Terraform will perform the following actions:

  # module.stg.google_bigquery_dataset.transfer_dest will be updated in-place
  ~ resource "google_bigquery_dataset" "transfer_dest" {
        id                              = "projects/project-name/datasets/dataset_name"
        # (11 unchanged attributes hidden)

      - access {
          - role          = "OWNER" -> null
          - user_by_email = "bq_upload_account@project-name.iam.gserviceaccount.com" -> null
        }
      + access {
          + role          = "roles/bigquery.dataOwner"
          + user_by_email = "bq_upload_account@project-name.iam.gserviceaccount.com"
        }
    }

Plan: 0 to add, 1 to change, 0 to destroy.

この後、再度applyして成功するも、更にplanを表示しても同じ状況になる。
つまり、"OWNER" -> nullの箇所が余分です。こんな指定は後にも先にも全くしていないのですが。
これは気持ち悪い・・・

解決方法

理屈はちゃんとわかっていませんが、以下のようにアクセス権付与をgoogle_bigquery_dataset_accessという別のリソースタイプでデータセットとは別個に指定してあげることで、当該事象は発生しなくなりました。

resource "google_bigquery_dataset" "transfer_dest" {
  dataset_id  = "transfer_dest"
  location    = "US"
}

resource "google_bigquery_dataset_access" "access" {
  dataset_id    = google_bigquery_dataset.transfer_dest.dataset_id
  role          = "roles/bigquery.dataOwner"
  user_by_email = google_service_account.bq_upload_account.email
}

resource "google_service_account" "bq_upload_account" {
  account_id = "bq_upload_account"
  description = "xxxxxxxxxxxxxx"
}

原因の想像

BQのデータセットをGUIポチポチで作ったりすると、プロジェクトレベルのIAMから継承がされたしてきますが、google_bigquery_datasetaccess項とかち合ってしまい、妙な状況になったりしているのかなと思いました。内部実装を見ていたりしていないので完全なる推測になります。

以上です!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0