user630702
user630702

Reputation: 3167

Terraform is using wrong google project

I deleted the project projects/project1-301106 and then created a new which ends with 301108. But terraform always try to use the old project.

Error: Error creating Network: googleapi: Error 404: The resource 'projects/project1-301106' was not found, notFound

I deleted .terraform directory and files, ran terraform init. Also cleared gcould and reauth but still the same error.

Upvotes: 4

Views: 2607

Answers (4)

Andrii Rusanov
Andrii Rusanov

Reputation: 4606

Another possible fix is rm ~/.terraform.d directory - this was an issue for me a few times (it only contained cache files in my case)

Upvotes: 0

user2314737
user2314737

Reputation: 29417

I don't use Gcloud but the Terraform Openstack provider gave me a hard time with a similar issue. No matter what I did, set Terraform variables, remove .terraform, remove terraform.tfstate and terraform.tfstate.backup (btw don't do it!!), run terraform init etc. I was always ending up creating my infrastructure in the wrong project.

... actually the infrastructure was always created in the last project I had been working on ...

And so just by chance I found out what the issue was: it was some environment variables that were somehow interfering (for OpenStack users, these are variables starting with OS). I needed these variables to work with the openstack command-line API client. My solution was to unset all "^OS" variables before working with Terraform.

I assume there might be a similar issue with GCloud, as there are some environment variables that are taken into account by Terraform (see provider_reference#provider-default-values-configuration):

  • GOOGLE_PROJECT
  • GOOGLE_CLOUD_PROJECT
  • GCLOUD_PROJECT
  • CLOUDSDK_CORE_PROJECT

Hope this helps!

Upvotes: 0

J W
J W

Reputation: 411

For me this was caused by running gcloud auth application-default login which opened in a browser tab using the wrong project. Removing terraform files did not solve this for me.

Upvotes: 1

Try this solution.

Remove these files and folders:

sudo rm -r .terraform .terraform.lock.hcl terraform.tfstate terraform.tfstate.backup

Then:

terraform init

Finally:

terraform apply -auto-approve

Upvotes: 2

Related Questions