- stop the compute engine instance
gcloud compute instances stop instance-1 --zone us-central1-c
- get the service account of the instance
gcloud compute instances describe instance-1 --zone us-central1-c | grep email
- assign the role
roles/compute.instanceAdmin
to the service account
gcloud projects add-iam-policy-binding your_project -- member="serviceAccount:SERVICE_ACCOUNT_ID@your_project.iam.gserviceaccount.com" --role="roles/compute.instanceAdmin"
- start the instance
gcloud compute instances start instance-1 --zone us-central1-c
- ssh to the instance
gcloud compute ssh instance-1 --zone us-central1-c
- run the gcloud command
gcloud compute instances list
Also read about the difference between IAM roles and OAuth scopes
When you set up an instance to run as a service account, you determine
the level of access the service account has by the IAM roles that you
grant to the service account. If the service account has no IAM roles,
then no API methods can be run by the service account on that
instance.
Furthermore, an instance's access scopes determine the default OAuth
scopes for requests made through the gcloud tool and client libraries
on the instance. As a result, access scopes potentially further limit
access to API methods when authenticating through OAuth