Review permissions of a VM’s default service account

Configure a service account and least-privilege permissions for the VM’s purpose.

Description

Attach a dedicated service account with the IAM roles each Compute Engine workload needs.

Potential impact

If multiple VMs share the default account, removing a role for one VM can interrupt the others.

Remediation

Set email under service_accounts to the dedicated account. To change an existing VM, stop it, attach the account, restart it and test the workload. auth_kind controls Ansible’s own authentication.

Examples

The revised task defines a VM named test-object. Apply the account change to test-object1 and test-object4 individually if those are the VMs you need to update. To create a VM, also supply a boot disk.

Before

yaml
- name: Create the 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: Create the 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

After

yaml
- name: Create the 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

References