VM のデフォルトサービスアカウントの権限を確認

VM の用途に合うサービスアカウントと、最小限の権限を設定します。

説明

Compute Engine の VM には、ワークロードに必要な IAM ロールだけを持つ専用サービス アカウントを割り当てます。

想定される影響

複数の VM がデフォルト アカウントを共有していると、1 台のためにロールを削除した際、他の VM の処理が止まることがあります。

対処方法

service_accounts の email に専用アカウントのメールアドレスを指定します。既存の VM では停止してからアカウントを変更し、再起動してワークロードを確認します。auth_kind は Ansible 自体の認証設定です。

例

変更後の例では test-object という別名の VM を定義しています。既存の test-object1 と test-object4 を更新する場合は、それぞれに変更を適用します。 新規作成時はブートディスクも指定します。

変更前

yaml
- name: test-object1 VM を作成
  google.cloud.gcp_compute_instance:
    name: test-object1
    machine_type: n1-standard-1
    zone: us-central1-a
    project: "{{ gcp_project_id }}"
    auth_kind: serviceaccount
    state: present

- name: test-object4 VM を作成
  google.cloud.gcp_compute_instance:
    name: test-object4
    machine_type: n1-standard-1
    zone: us-central1-a
    project: "{{ gcp_project_id }}"
    auth_kind: serviceaccount
    service_accounts:
      - email: "{{ project_number }}-compute@developer.gserviceaccount.com"
    state: present

変更後

yaml
- name: VM を作成
  google.cloud.gcp_compute_instance:
    name: test-object
    machine_type: n1-standard-1
    zone: us-central1-a
    project: "{{ gcp_project_id }}"
    auth_kind: serviceaccount
    service_accounts:
      - email: "{{ vm_service_account_email }}"
        scopes:
          - https://www.googleapis.com/auth/cloud-platform
    state: present

参考資料