31

I am learning terraform. I want to print values of variables in "plan" stage. So I found how to do it. seems I am doing something wrong here....

in variables.tf:....

variable "VMCount" {
    description = "How many VMs do you want to start with (number)? default=1 max=5"
    type = number
}

in main.tf

output "VMCount" {
  value = "${var.VMCount > 2 && var.VMCount < 6 ? var.VMCount : 2}"
}

after that i run terraform plan and the condition seem to be working fine (it creates right num of VMs)

but the variable output is not coming. why?

$ terraform output
VMC = 56

that VMC is might be from some previous attempts ( I tried several things).

How to print the value of user entry (variable)?

2
  • 1
    Your code doesn't match the output shown. Specifically if you set VMCount to 56 it will currently return 2. Can you please edit your question to show your actual code (ideally as a minimal reproducible example) with the actual output you get when you run terraform apply and terraform output? Commented Jun 17, 2021 at 11:04
  • 2
    In simple cases, you can use terraform init and followed by terraform console. Then use console expressions to run & evaluate them. Commented Aug 25, 2023 at 7:57

4 Answers 4

24

I tested with this:

variable "VMCount" {
    description = "How many VMs do you want to start with (number)? default=1 max=5"
    type = number
}

output "VMCount" {
  value = "${var.VMCount > 2 && var.VMCount < 6 ? var.VMCount : 2}"
}

and it works well.

Terraform will perform the following actions:

Plan: 0 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + VMCount = 4

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes


Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

VMCount = 4
PS C:\d\m\terraform\output-and-variable> terraform output
VMCount = 4
PS C:\d\m\terraform\output-and-variable> terraform apply
var.VMCount
  How many VMs do you want to start with (number)? default=1 max=5

  Enter a value: 8


Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:

Terraform will perform the following actions:

Plan: 0 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  ~ VMCount = 4 -> 2

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes


Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

VMCount = 2
PS C:\d\m\terraform\output-and-variable> terraform output
VMCount = 2
PS C:\d\m\terraform\output-and-variable>

Could you check what outouts doyou have in state? VMC or VMCount?

Sign up to request clarification or add additional context in comments.

3 Comments

How do I check that?
Do you have remote backend or where do you store your state? Open state file and you should find output near the top.
Only somewhat related, but I came across this question while looking to inspect module variables and I learned you can do that with Terraform console. terraform.io/cli/commands/console
8

As stated here in the terraform documentation:

Outputs are only rendered when Terraform applies your plan. Running terraform plan will not render outputs.

2 Comments

I don't believe this is true, I have seen outputs rendered to the terminal after running terraform plan
@Damo Correct, terraform plan does print a Changes to Outputs: at least in v1.3.4. I don't know if it still prints the outputs when there is no change.
5

My answer is disregarding the context of the original question (which has already been answered in the past), but only focusing on the question in the title:

How to print terraform variable values?

And I want to correct something that is implied by all these other answers, i.e. that a terraform variable can only be retrieved programmatically via the outputs, and thus needing to terraform apply first. This is not true. You only need terraform init beforehand.

There is a way to print an actual variable (i.e. from variables.tf and/or terraform.tfvars) It is a bit hidden, there is no direct subcommand designed for this (as opposed to terraform output) but you can do it with this short "one-liner":

terraform console <<EOF
var.<your_variable>
EOF

2 Comments

this doesn't work - after terraform init and terraform plan when I try terraform console and ask for var.location it says (known after apply)
@Michu93 what is your definition of the variable ?
3

I tested with Terraform v1.3.4. When outputs are defined, terraform plan appears to print them as a final section after the resource changeset. It starts with Changes to Outputs:.

I don't know if it will still work when the outputs haven't changed. The easy solution is to simply add some dummy string to them to artificially create a change.

In your question, the value expression is quite complex. There may be bugs in that which are causing the value to not be printed. I think that's something for a separate question, because many people who come to this question looking for a way to print outputs will not have the same complex expression. You should see the outputs printed when you use:

output "example" {
  value = "this should show up in plan, at least until you apply it"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.