Terraform GCP Error – Could not find default credentials
Error: google: could not find default credentials.
for more information.
on line 0:
(source code not available)
- Client Libraries can be used to call GCP APIs
- All one needs is the Cloud SDK (see below) – which install client libraries locally on your desktop.
install Google Cloud SDK on your development desktop
- On windows, just open up a powershell prompt and use this:
(New-Object Net.WebClient).DownloadFile("https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe", "$env:Temp\GoogleCloudSDKInstaller.exe") & $env:Temp\GoogleCloudSDKInstaller.exe
- Once installed, run the following
gcloud
commands
gcloud init --> THis will prompt you to use an existing gmail account or a different one gcloud auth application-default login
This command will generate an ADC (Application Default Credentials) JSON file based on your user (IAM user) account and store it in a location where the SDK can find it automatically.
You should also receive a ‘security alert’ email from google – Google Auth Library was granted access to your Google Account myuser@gmail.com
What if you need to switch your human (gmail) or service account identities ? Or to do this from another PC?
You need the command below to re-prompt a login ( This will allow you to switch gmail identites ).
gcloud auth application-default login
Another possible error – Callers must accept terms of service…
When you try to create a new project (gcloud create project myproject),
gcloud projects create myprojectname
The Quick Fix for Callers must accept terms of service..
Log into the console with the same gSuite account and create a project by selecting ‘select a project’ (and ‘Create a Project’).
This will prompt the terms of service agreement and you may carry on after agreeing to the terms of service.
If you are using Cloud Shell, you are logged in as your IAM user. The IAM (Human) User is what logs on to GCP (via cloud shell).
Appendix A – Some Roles your Human or Service Account identity may need on GCP for successfully creating Terraform Resources
-
- iam.serviceAccounts.actAs permission for your project – e.g. ‘projects/my-awesome-project’
- If you plan to use the default compute engine SA, you would need to be granted the role – serviceAccountUser for that SA. Ask a project owner to grant you the iam.serviceAccountUser role on the service account
- If you will be creating your own SAs and assigning roles to them,
- resourcemanager.projects.getIamPolicy on the my-awesome-project
- resourcemanager.projects.setIamPolicy on the same project
- iam.roles.list – on the same project.
Leave a Reply