hashicorp / hashicorp/web-unified-docs

uuid() does not run during the refresh/plan phase

Open
#780 2 comments 0 reactions 0 assignees View on GitHub
documentation Terraform CE
Dominant language
MDX
Stars
86
Forks
275
Avg merge
3d 18h
Merged PRs (30d)
199

Description

### Terraform Version

```
Terraform v1.0.4
on windows_amd64
+ provider registry.terraform.io/hashicorp/null v3.1.0
```

### Terraform Configuration Files

```terraform
locals {
my_id = uuid()
}

resource "null_resource" "test" {
for_each = {
(local.my_id) = "test"
}
}
```

### Debug Output

https://gist.github.com/KyleKotowick/c09f4a598f5b26e86fb5a726e6bd3d7c

### Expected Behavior
Since the [uuid](https://www.terraform.io/docs/language/functions/uuid.html) function generates a value without storing it in state, and gets a new value each time it's run, it can be considered to be most similar to a data source.

Per the [data source documentation](https://www.terraform.io/docs/language/data-sources/index.html),:
> If the query constraint arguments for a data resource refer only to constant values or values that are already known, the data resource will be read and its state updated during Terraform's "refresh" phase, which runs prior to creating a plan.

Additionally, other functions such as [uuidv5](https://www.terraform.io/docs/language/functions/uuidv5.html) and [sha512](https://www.terraform.io/docs/language/functions/sha512.html) generate and return their value during the refresh phase.

Therefore, one would expect that the `uuid()` function should generate and return its value during the refresh phase as well.

### Actual Behavior
Running the sample code above will return the following error:
```
Error: Invalid for_each argument

│ on main.tf line 6, in resource "null_resource" "test":
│ 6: for_each = {
│ 7: (local.my_id) = "test"
│ 8: }
│ ├────────────────
│ │ local.my_id is a string, known only after apply

│ The "for_each" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the -target argument to first apply only the resources that the for_each depends on.
```

The line saying `local.my_id is a string, known only after apply` highlights the issue; the value of `uuid()` isn't known until the apply phase, which is inconsistent with data sources and other functions.

### Steps to Reproduce
1. `terraform init`
2. `terraform apply`

### Additional Context
I've tested this on Linux as well, with the same result.

Contributor guide

Open the contributing guide

Research direction

Start with the Terraform configuration shown as main.tf and reproduce the issue using terraform init and terraform apply. Compare uuid() with uuidv5() and sha512() during the refresh phase, then identify whether the relevant Terraform behavior or documentation entry point is present in this repository. Done requires a confirmed resolution for the reported inconsistency.

Written by the indexing model from the issue text.

Assessment

Tech stack
terraform
Domain
documentation
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.