Terraform/GCP Error: project: required field is not set

Viewed 4265

Problem

The google_project document says the project_id is optional.

project_id - (Optional) The project ID. If it is not provided, the provider project is used.

However, Terraform complains it is required.

gcp.tf

data "google_project" "project" {
}

output "project_number" {
  value = data.google_project.project.number
}
 Error: project: required field is not set
│ 
│   with data.google_project.project,
│   on gcp.tf line 1, in data "google_project" "project":
│    1: data "google_project" "project" {

Question

Please help understand if this is a documentation defect and the argument is mandatory actually.

Workaround

Set the GOOGLE_PROJECT environment variable.

export GOOGLE_PROJECT=...
terraform apply
2 Answers

Your 'Workaround' is functionally equivalent to what the documentation suggests. Namely that the provider project should be set, i.e.:

provider "google" {
  project = "..."
}

You don't include your provider config but, I assume, it doesn't include the default project to be used.

So, either|or but, somewhere you need to define the default project.

Otherwise, you should expect to get the error.

Even i faced this issue and then dug deep into this, and found this solution. It worked for me.

I Hope it works for u too.

In the provider.tf file.

provider "google" {
project  = "<PROJECT GCP ID>"
}
Related