Order of resource creation and output

Hello,
does outputstatement get executed in the order it is encountered in the source file or is it postponed until the end of the resources’ creation (end of the source file)?

Hi @AZZ,

Terraform uses a “data flow” execution model where the order of operations is decided by the reference expressions written in the configuration for each object. The order of declarations in your source files has no effect on execution and in fact isn’t even visible to the runtime engine. (The configuration loader produces unordered maps as its result data structure.)

For example, consider the following configuration:

resource "aws_instance" "example" {
  # ...
}

output "instance_id" {
  value = aws_instance.example.id
}

output "greeting" {
  value = "Hello!"
}

The output value “instance_id” has a value expression that refers to aws_instance.example, so Terraform infers that the actions for that resource must happen before deciding the output value’s result.

However, the output value “greeting” does not refer to anything else, so Terraform assumes it can decide its result immediately without waiting for anything else to happen first.

In situations like the second output value, there are no constraints whatsoever on when that output value can be calculated, and so Terraform might calculate it concurrently with some other work, immediately before doing anything else, or at any other time.

In practice output values don’t have any side-effects outside of Terraform, so it isn’t really important exactly when they get calculated. But this rule is also true for references between resource configuration blocks too, and those do represent side-effects and thus using references to describe the dependency between resources is an important part of writing a Terraform module.

@apparentlymart ,
thank you for, as always, a very detailed explanations. It helps and it makes sense.

Unfortunately, now , i do not know what to think about my current situation. I’ll do a few more runs to understand better what is happening.

Thank you @apparentlymart .
AZ