01 Run iam service-accounts create command (Windows/macOS/Linux) to create a new Google Cloud Platform (GCP) service account. The following iam service-accounts create request example, creates a service account named "cc-web-stack-service-account", for a GCP project named "cc-web-stack-project-123123":
gcloud iam service-accounts create cc-web-stack-service-account
--display-name="Web Stack Instance Service Account"
--project cc-web-stack-project-123123
--format="table(email)"
02 The command output should return the email address of the new GCP service account:
Created service account [cc-web-stack-service-account].
EMAIL
cc-web-stack-service-account@cc-web-stack-project-123123.iam.gserviceaccount.com
03 Run add-iam-policy-binding command (Windows/macOS/Linux) to grant the appropriate IAM role to the newly created GCP service account in order to allow that service account access to relevant API methods. The following command request example applies the App Engine Code Viewer IAM role (i.e. "roles/appengine.codeViewer") to a service account identified by the email address "cc-web-stack-service-account@cc-web-stack-project-123123.iam.gserviceaccount.com". The new role assignment follows the principle of least privilege (POLP) and provides the selected service account only the ability to view App Engine application status and deployed source code:
gcloud projects add-iam-policy-binding cc-web-stack-project-123123
--member serviceAccount:cc-web-stack-service-account@cc-web-stack-project-123123.iam.gserviceaccount.com
--role roles/appengine.codeViewer
04 The command output should return the updated project IAM policy:
Updated IAM policy for project [cc-web-stack-project-123123].
bindings:
- members:
- serviceAccount:cc-web-stack-service-account@cc-web-stack-project-123123.iam.gserviceaccount.com
role: roles/appengine.codeViewer
- members:
- serviceAccount:service-123456789012@compute-system.iam.gserviceaccount.com
role: roles/compute.serviceAgent
- members:
- serviceAccount:123456789012-compute@developer.gserviceaccount.com
role: roles/editor
- members:
- user:admin@cloudconformity.com
role: roles/monitoring.admin
- members:
- user:admin@cloudconformity.com
role: roles/owner
etag: ABCDABCDABCD
version: 1
05 Run compute instances stop command (Windows/macOS/Linux) using the name of the VM instance that you want to reconfigure as identifier parameter (see Audit section part II to identify the instance that uses the default Compute Engine service account), to stop the selected instance:
gcloud compute instances stop cc-web-stack-instance
--zone=us-central1-a
06 The command output should return the compute instances stop command request status:
Stopping instance(s) cc-web-stack-instance...done.
Updated [https://compute.googleapis.com/compute/v1/projects/cc-web-stack-project-123123/zones/us-central1-a/instances/cc-web-stack-instance].
07 Run compute instances set-service-account command (Windows/macOS/Linux) to associate the GCP service account created at the previous steps with the selected Google Compute Engine instance. This account represents the service account that the instance uses when calling Google Cloud APIs:
gcloud compute instances set-service-account cc-web-stack-instance
--zone=us-central1-a
--service-account=cc-web-stack-service-account@cc-web-stack-project-123123.iam.gserviceaccount.com
08 The command output should return the URL of the reconfigured VM instance:
Updated [https://www.googleapis.com/compute/v1/projects/cc-web-stack-project-123123/zones/us-central1-a/instances/cc-web-stack-instance].
09 Run compute instances start command (Windows/macOS/Linux) to restart the reconfigured Google Compute Engine instance:
gcloud compute instances start cc-web-stack-instance
--zone=us-central1-a
10 The command output should return the compute instances start command request status:
Starting instance(s) cc-web-stack-instance...done.
Updated [https://compute.googleapis.com/compute/v1/projects/cc-web-stack-project-123123/zones/us-central1-a/instances/cc-web-stack-instance].
Instance internal IP is 10.0.0.2
Instance external IP is 10.10.10.5
11 If required, repeat steps no. 1 – 10 to reconfigure other virtual machine (VM) instances created within the selected project.
12 Repeat steps no. 1 – 11 for each GCP project deployed in your Google Cloud account.