Create Service Account for Terraform on GCP
Create Service Account for Terraform on GCP We’ll run this procedure using Cloud Shell or the gcloud SDK. Variable $PROJECT_ID Let’s set the $PROJECT_ID variable with the name of our GCP project: xport PROJECT_ID="your-project-id"echo $PROJ
Source comparison and review notes
- Automated pair-review warning (AI bilingual content review): Both sources grant roles/editor and roles/resourcemanager.projectIamAdmin to the Terraform Service Account, which are broad and potentially privilege-escalating permissions despite the accompanying least-privilege note.
- Automated pair-review warning (AI bilingual content review): The English initial command is rendered as xport PROJECT_ID="your-project-id" (missing the e), whereas Spanish shows export; the English standalone command would fail.
- Automated pair-review warning (AI bilingual content review): Both use a downloadable Service Account JSON key; retaining it in source is required here, but this authentication approach needs security review.

We’ll run this procedure using Cloud Shell or the gcloud SDK.
Variable $PROJECT_ID
Let’s set the $PROJECT_ID variable with the name of our GCP project:
xport PROJECT_ID="your-project-id"echo $PROJECT_IDRun the command in Cloud Shell, and you can check the contents of the variable.

Then we’ll run the following command to create the Service Account (SA):
gcloud iam service-accounts create terraform --display-name="Terraform"Next, we’ll run the gcloud projects add-iam-policy-binding command to give the necessary permissions:
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:terraform@$PROJECT_ID.iam.gserviceaccount.com" --role="roles/editor"gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:terraform@$PROJECT_ID.iam.gserviceaccount.com" --role="roles/compute.networkAdmin"gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:terraform@$PROJECT_ID.iam.gserviceaccount.com" --role="roles/resourcemanager.projectIamAdmin"Full Script:
gcloud iam service-accounts create terraform --display-name="Terraform" && gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:terraform@$PROJECT_ID.iam.gserviceaccount.com" --role="roles/editor" && gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:terraform@$PROJECT_ID.iam.gserviceaccount.com" --role="roles/compute.networkAdmin" && gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:terraform@$PROJECT_ID.iam.gserviceaccount.com" --role="roles/resourcemanager.projectIamAdmin"When you run it, you’ll see the permissions generated.

In this case, we assign the roles of Editor and Compute Network Admin. In a production use case, we must use the principle of Least Privilege.

Activate Resource Manager API
To use Terraform on GCP, we need to make sure that the “Cloud Resource Manager” API is enabled. This must be done manually.
- Go to the GCP Console search engine and search for the API.

- Enable the API.

- Verify that it is activated.

Get credentials to connect Terraform
For Terraform to connect to our GCP account, we must generate a credential file. In this case, we’ll use one in JSON format.
- Go to “IAM & Admin” in our console and log in to the account we just created.

- Go to the “Keys” tab and add a new key.

- Select “Create new key”.

- Select JSON format and click Enable.

- This will download a .json file with our Service Account credentials.
IMPORTANT
At the end, the json file will be downloaded with access to our Service Account.

Configure Environment Variable GOOGLE_APPLICATION_CREDENTIALS
Now we need to run the following command in our Terraform runtime environment. In this case, we will use an environment with local state in vscode for the lab environment.
- First, make sure that we have our credential file securely available.
- Then, run the following command in PowerShell:
$env:GOOGLE_APPLICATION_CREDENTIALS="multicloudstech3-88dccdcecd98.json"- When you run the command, Terraform will enable the ability to use these credentials to log in to GCP.

Before committing the code, we need to make sure to include our credential files in the .gitignore file.

Mateo Di Loreto
Thanks for reading. The learning continues in the next article.


