LoginSignup
18
8

More than 3 years have passed since last update.

Terraform x GCP で、IAM権限を全削除してしまった

Last updated at Posted at 2020-01-07

我が社では最近GCPを使っています。

初めはWEB画面でポチポチ設定していたのですが、規模が大きくなってきたのでTerraformを導入することにしました。

とりあえず、直近で必要なサービスアカウントをTerraformで作ることにします。


resource "google_service_account" "logagg" {
  account_id = "logagg-fluentd"
  display_name = "logagg-fluentd"
  description = "fluentd が Cloud Pub/Sub にデータを挿入するためのアカウント"
}

resource "google_project_iam_policy" "pubsub-publisher" {
  project = "my-project"
  role = "roles/pubsub.publisher"
  members = [
    "serviceAccount:${google_service_account.logagg.email}"
  ]
}

terraform applyyes を実行!

ドーン!!

誰もGCPの画面にログインできなくなりました。

特権管理者になって確認すると、全員のIAM権限が削除され、terraformで作ったサービスアカウントの権限だけが残っていました。

なお、吹っ飛んだのはテスト環境だったので実損はありませんでした。

何が起こったの?

Terraformのgoogle_project_iam_policyのドキュメントには、以下のような説明があります。

google_project_iam_policy: Authoritative. Sets the IAM policy for the project and replaces any existing policy already attached.

google_project_iam_binding: Authoritative for a given role. Updates the IAM policy to grant a role to a list of members. Other roles within the IAM policy for the project are preserved.

私訳

google_project_iam_policy: 権限。プロジェクトのIAMポリシーを設定し、既にアタッチされている既存のポリシーを置き換えます。

google_project_iam_binding: 指定したロールについての権限。
IAMポリシーを更新して、メンバーのリストにロールを付与します。プロジェクトのIAMポリシー内の他のロールについては現状維持です。

つまり、
google_project_iam_policyを使うと「プロジェクトの全IAM権限をTerraformで管理する」ということになり、Terraform外の設定は全て破棄されます。

今回のように既にIAMの設定がある場合は google_project_iam_binding を使うべきだったのです。

むすび

ドキュメントは読みましょう。

18
8
1

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
18
8