To access Google Cloud resources, Compute Engine virtual
machine (VM) instances use
[service accounts](https://docs.cloud.google.com/iam/docs/service-account-overview.md.txt). If you
accidentally delete the
[Compute Engine default service account](https://docs.cloud.google.com/compute/docs/access/service-accounts.md.txt#default_service_account),
applications that run on your VMs might not be able to make calls to
Google Cloud APIs.

This document explains how to recover the Compute Engine default service
account after it is deleted.

## Recover the Compute Engine default service account

When you delete a service account, Identity and Access Management (IAM)
permanently removes the service account after 30 days. If you accidentally
delete the Compute Engine
default service account, Google Cloud cannot recover the service account
after it is permanently removed and then the VMs will no longer have access to
resources in the project. In such cases, if you want to assign a
Compute Engine default service account for the VM, create a service account
and set it as the default service account.


To get the permissions that
you need to perform this task,

ask your administrator to grant you the
following IAM roles on your project:

- [Compute Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/compute.md.txt#compute.admin) (`roles/compute.admin`)
- [Logs Viewer](https://docs.cloud.google.com/iam/docs/roles-permissions/logging.md.txt#logging.viewer) (`roles/logging.viewer`)


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


You might also be able to get
the required permissions through [custom
roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).

To recover a deleted Compute Engine default service account, do the following:

1. In the Google Cloud console, go to the **Logs Explorer** page.

   [Go to Logs Explorer](https://console.cloud.google.com/logs/query)

   Use the following query to validate when the Compute Engine default service
   account was deleted:

   ```
   resource.type="service_account"
   protoPayload.methodName="google.iam.admin.v1.DeleteServiceAccount"
   "PROJECT_NUMBER-compute@developer.gserviceaccount.com"
   ```

   Replace `PROJECT_NUMBER` with the project number of your
   project.

   **If the default service account was deleted less than 30 days ago**:

   Use the `undelete` command to recover the service account. For more
   information, see [Undelete a service
   account](https://docs.cloud.google.com/iam/docs/service-accounts-delete-undelete#undeleting).

   **If the default service account was deleted more than 30 days ago**:
   1. [Create a service account](https://docs.cloud.google.com/iam/docs/service-accounts-create.md.txt#creating) or
      select an existing service account to set as the new Compute Engine
      default service account.

   2. Use the
      [`gcloud alpha compute project-info set-default-service-account`](https://docs.cloud.google.com/sdk/gcloud/reference/alpha/compute/project-info/set-default-service-account.md.txt)
      command to set the new service account as the default service account for
      your project:

      ```
      gcloud alpha compute project-info set-default-service-account \
        --service-account=SERVICE_ACCOUNT_EMAIL
      ```

      Replace `SERVICE_ACCOUNT_EMAIL` with the email
      address of the service account that you created.
2. If you want VMs to use the newly created default service account, [follow these
   steps](https://docs.cloud.google.com/compute/docs/instances/change-service-account#changeserviceaccountandscopes):

   1. Go to the **VM instances** page.

      [Go to VM instances](https://console.cloud.google.com/compute/instances)
   2. Click the VM instance name for which you want to use the service account.

   3. If the VM is running, click **Stop**
      to stop the VM. If there is no **Stop** option, click
      **More actions \>** **Stop**.

   4. Click **Edit**.

   5. Scroll down to the **Service Account** section.

   6. From the drop-down list, select the service account to assign to the
      instance.