
output can be used to output text to the console.
- output a string, integer, boolean or variable (this article)
- output the value or JSON returned from a data block
- output the value or JSON returned from a resource block
AVOID TROUBLE
only output from the root module will be displayed by commands such as terraform refresh, terraform plan, terrafrom apply and terraform output
Let's say you have the following files on your Terraform server.
├── locals.tf
├── modules.tf
├── outputs.tf
├── provider.tf
├── terraform.tfstate
├── variables.tf
├── child (directory, child module)
│ ├── data.tf
│ ├── outputs.tf
│ ├── resources.tf
And lets say outputs.tf in the same directory as your main root module (main.tf) contains the following. In this example, output is used to output
output "my-string" {
value = "Hello World"
}
output "my-integer" {
value = 1
}
output "my-boolean" {
value = true
}
The terraform refresh, terrafrom plan, or terraform apply commands should now print the output.
terraform refresh
Which should return the following.
my-string = "Hello World"
my-integer = 1
my-boolean = true
terraform.tfstate | terraform output
And now your terraform.tfstate should contains something like this in the "outputs" key.
{
"version": 4,
"terraform_version": "1.2.6",
"serial": 90,
"lineage": "10e7c02c-ce6a-0ace-c0d5-57a516c6d706",
"outputs": {
"my-string": {
"value": "Hello World",
"type": "string"
},
"my-integer": {
"value": 1,
"type": "number"
},
"my-boolean": {
"value": true,
"type": "bool"
}
}
}
Going forward, the terraform output command will return the outputs in your terraform.tfstate file.
my-string = "Hello World"
my-integer = 1
my-boolean = true
Variables
return the value of a variable to the console.
Variables can be defined in a variable block or locals block. For example, let's say your variables.tf file has the following.
variable "string" {
type = string
sensitive = false
default = "Goodbye World"
}
variable "integer" {
type = number
sensitive = false
default = 2
}
variable "boolean" {
type = bool
sensitive = false
default = false
}
And let's say outputs.tf has the following.
output "var-string" {
value = var.string
}
output "var-integer" {
value = var.integer
}
output "var-boolean" {
value = var.boolean
}
The terraform refresh, terrafrom plan, or terraform apply commands should now print the output.
terraform refresh
Which should return the following.
var-string = "Goodbye World"
var-integer = 2
var-boolean = false
And now your terraform.tfstate should contains the variables so that going forward, the terraform output command will return the outputs in your terraform.tfstate file.
var-string = "Goodbye World"
var-integer = 2
var-boolean = false
Did you find this article helpful?
If so, consider buying me a coffee over at