You can use Google Cloud Shell or your local workstation to complete these steps.
Set the environment variable for the service account name:
export SERVICE_ACCOUNT=imagebuilder
Set the environment variables for the GCP project name:
export PROJECT=<YOUR GCP PROJECT>
To create the service account, run the following command:
gcloud iam service-accounts create $SERVICE_ACCOUNT \
--display-name "VM Imagebuilder service account" \
--project $PROJECT
To create and download the service account key, run the following command:
gcloud iam service-accounts keys create $SERVICE_ACCOUNT-service-account.json \
--project $PROJECT \
--iam-account $SERVICE_ACCOUNT@$PROJECT.iam.gserviceaccount.com
The service account JSON key is created and downloaded as $SERVICE_ACCOUNT-service-account.json
.
To grant permissions to the service account, run the following commands:
gcloud projects add-iam-policy-binding $PROJECT \
--member serviceAccount:$SERVICE_ACCOUNT@$PROJECT.iam.gserviceaccount.com \
--role roles/compute.instanceAdmin.v1
gcloud projects add-iam-policy-binding $PROJECT \
--member serviceAccount:$SERVICE_ACCOUNT@$PROJECT.iam.gserviceaccount.com \
--role roles/storage.objectAdmin
gcloud projects add-iam-policy-binding $PROJECT \
--member serviceAccount:$SERVICE_ACCOUNT@$PROJECT.iam.gserviceaccount.com \
--role roles/iam.serviceAccountUser