Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data.google_project.project.project_id sometimes null #10587

Closed
asnare opened this issue Nov 18, 2021 · 6 comments
Closed

data.google_project.project.project_id sometimes null #10587

asnare opened this issue Nov 18, 2021 · 6 comments
Assignees
Labels

Comments

@asnare
Copy link

asnare commented Nov 18, 2021

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request.
  • Please do not leave +1 or me too comments, they generate extra noise for issue followers and do not help prioritize the request.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.
  • If an issue is assigned to the modular-magician user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If an issue is assigned to a user, that user is claiming responsibility for the issue. If an issue is assigned to hashibot, a community member has claimed the issue already.

Terraform Version

Terraform v1.0.11
on darwin_amd64
+ provider registry.terraform.io/hashicorp/google v4.1.0
+ provider registry.terraform.io/hashicorp/null v3.1.0

Affected Resource(s)

  • google_project (data source)

Terraform Configuration Files

A module is needed to demonstrate the problem; this is the minimum set of files I needed to reproduce the issue:

  • main.tf
  • modules/problem/main.tf
#main.tf

# Here's a resource that we're going to depend on.
resource "null_resource" "dummy" {}

# Here's a module that will demonstrate the issue.
module "problem" {
  source = "./modules/problem"

  # If this line:
  #  - Is present, the project_id from the data source in the module is null.
  #  - Is commented-out, the project_id from the data source in the module has its expected value.
  depends_on = [null_resource.dummy]
}

terraform {
  required_providers {
    google = {
      version = "~> 4.1.0"
      source  = "hashicorp/google"
    }
  }
  backend "local" {}
}

variable "project" {
  type = string
}

provider "google" {
  project = var.project
}
#modules/problem/main.tf

# The project_id on this data source ends up sometimes as null.
data "google_project" "problematic_project" {}

resource "null_resource" "problematic_resource" {
  triggers = {
    # Contrived string interpolation, triggers the error.
    project = "String with interpolated project ID: ${data.google_project.problematic_project.project_id}"
  }
  provisioner "local-exec" {
    command = "echo 'Project ID: %s' \"${data.google_project.problematic_project.project_id}\""
  }
}

Debug Output

Some debug output is here, produced by running: TF_LOG=DEBUG terraform plan -no-color -var project_id=MASKED

Panic Output

No panic is involved.

Expected Behavior

The google_project data source inside the module should have a usable project_id attribute, irrespective of whether the module depends on another resource/module or not.

Actual Behavior

If the enclosing module instantiation has a non-empty depends_on property, for some reason the data source inside the module has null for the project_id. This triggers errors such as seen here, where string interpolation fails because null isn't allowed in that context. If the depends_on clause is removed then the data source/property all seem to work properly and provide the correct project id.

Steps to Reproduce

  1. terraform plan -var project=a_project_id
  2. Observe failure to plan due to null project_id from the data source.
  3. Comment out the depends_on property where the module is being used.
  4. terraform plan -var project=a_project_id
  5. Now it worked.

Important Factoids

N/A

References

A potential earlier report of the same thing:

@asnare asnare added the bug label Nov 18, 2021
@edwardmedia edwardmedia self-assigned this Nov 18, 2021
@edwardmedia
Copy link
Contributor

edwardmedia commented Nov 18, 2021

@asnare using your code, it seems fine with you. After I provided myproject for the following variable, I do see below line in the debug log, and not see the error as you mentioned. What did I miss?

module.problem.null_resource.problematic_resource (local-exec): Executing: ["/bin/sh" "-c" "echo 'Project ID: %s' "myproject""]

variable "project" {
  type = string
}

provider "google" {
  project = var.project
}

@asnare
Copy link
Author

asnare commented Nov 18, 2021

@edwardmedia Hmm. And that is with the depends_on = line in place?

With it in place, the project_id property goes to null and I get the following error message:

│
│   on modules/problem/main.tf line 6, in resource "null_resource" "problematic_resource":
│    6:     project = "String with interpolated project ID: ${data.google_project.problematic_project.project_id}"
│     ├────────────────
│     │ data.google_project.problematic_project.project_id is null
│
│ The expression result is null. Cannot include a null value in a string template.

I get the same results when running on two laptops against different GCP subscriptions/accounts. The versions are the identical for both; I get it with provider 4.0.0 and 4.1.0.

The lock-file for the debug log I made is:

# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.

provider "registry.terraform.io/hashicorp/google" {
  version     = "4.1.0"
  constraints = "~> 4.1.0"
  hashes = [
    "h1:eqp/ZZ0N2tF0ZSaAUdS6b5TLSZKPegmFLqEFid89V9k=",
    "zh:3477415ef4ef02d7c065a08fb24b7011ddd77f76b2a011db25e0b73c767acaae",
    "zh:43be16d0bbb56d29c090c1abe49e877e4f3ca66bb372a661a2cd0a377cd61e8f",
    "zh:6bb202a40055ed302f9dd05e2f3f87aa13080e580a9ac7b0241d634b78b68a2a",
    "zh:7179303f628c5fc58129013ef4a4e754e225e266e68af4a7167a06a64d9d97db",
    "zh:837cdcf1bafd4c936f15a4a22353efeec96ca23727f8d8ff22e9d53414d87084",
    "zh:a853f1a1e9f5ac261fd9a02d869ce7da16917fdd16464dabf2200e2ccd04fba9",
    "zh:aba631b9db47cc34857c25459475f17d939a46022f886e396a8254588f720c58",
    "zh:ac461358984ba480b4fef1d2be3538d9714aa8814a6f420e1e954227054a14a4",
    "zh:ade64689485fc28b6aa0c36861c6c9c66b5d7a8e7282739ecf97c7791b973312",
    "zh:c3fbbfe7f6e870e19bb552a2ad84ea38c9ddf3e25e0b7e996ecb2fc7ce336b03",
    "zh:d1419f4fe9c07f136ee9015a299cd6acf25a13bea0afa0f69727a576058603d3",
  ]
}

provider "registry.terraform.io/hashicorp/null" {
  version = "3.1.0"
  hashes = [
    "h1:xhbHC6in3nQryvTQBWKxebi3inG5OCgHgc4fRxL0ymc=",
    "zh:02a1675fd8de126a00460942aaae242e65ca3380b5bb192e8773ef3da9073fd2",
    "zh:53e30545ff8926a8e30ad30648991ca8b93b6fa496272cd23b26763c8ee84515",
    "zh:5f9200bf708913621d0f6514179d89700e9aa3097c77dac730e8ba6e5901d521",
    "zh:9ebf4d9704faba06b3ec7242c773c0fbfe12d62db7d00356d4f55385fc69bfb2",
    "zh:a6576c81adc70326e4e1c999c04ad9ca37113a6e925aefab4765e5a5198efa7e",
    "zh:a8a42d13346347aff6c63a37cda9b2c6aa5cc384a55b2fe6d6adfa390e609c53",
    "zh:c797744d08a5307d50210e0454f91ca4d1c7621c68740441cf4579390452321d",
    "zh:cecb6a304046df34c11229f20a80b24b1603960b794d68361a67c5efe58e62b8",
    "zh:e1371aa1e502000d9974cfaff5be4cfa02f47b17400005a16f14d2ef30dc2a70",
    "zh:fc39cc1fe71234a0b0369d5c5c7f876c71b956d23d7d6f518289737a001ba69b",
    "zh:fea4227271ebf7d9e2b61b89ce2328c7262acd9fd190e1fd6d15a591abfa848e",
  ]
}

I'm a bit perplexed as to why you're not seeing the same error. Is there any obvious difference in your DEBUG log? :-/

@edwardmedia
Copy link
Contributor

@asnare I see what you mean. As you can see the difference between having depends_on and not, I think this is the question to Terraform SDK (vs the Terraform provider). In term of the data source, it works as it is designed. Does this make sense?

@edwardmedia
Copy link
Contributor

@asnare closing the issue now based on the behavior observed. Feel free to reopen if you have further questions

@TobKed
Copy link

TobKed commented Dec 2, 2021

I have the same issue with random null for data.google_project.project.number.

It worked for a few days, everything was running smoothly and today, out of nowhere I got:

│     │ data.google_project.project.number is null
│
│ The expression result is null. Cannot include a null value in a string template.

I spent 3 hours trying to figure out what was wrong, restarting the computer, playing with providers’ versions, digging up open and closed issues. Nothing helped. Returned to the version which worked perfectly yesterday, retried a few times without success, and then... magically, it started working again 🤷.

Terraform v1.0.11
on darwin_amd64
+ provider registry.terraform.io/hashicorp/google v4.1.0

Extracted sample:

terraform {
  required_version = ">= 1.0.10"
}

provider "google" {
  project = var.project_id
  region  = var.region
  zone    = var.zone
}

data "google_project" "project" {}

resource "google_project_iam_binding" "secret_manager_secret_accessor" {
  project = var.project_id
  role    = "roles/secretmanager.secretAccessor"

  members = [
    "serviceAccount:${data.google_project.project.number}@cloudbuild.gserviceaccount.com"
  ]
}

@github-actions
Copy link

github-actions bot commented Jan 2, 2022

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants