Updated on 2023-12-13 GMT+08:00

Output Variables

Output variables are like return values for a module. They are declared using the keyword output. Output variables can expose certain information. They can be used by a root module to output certain values after running the terraform apply/output command, or by a child module to expose a subset of its resource attributes to a parent module.

Declaring Output Variables

By convention, output variables are defined in a file named variables.tf. Output variables are declared using the keyword output.

output "ecs_address" {
  description = "The private IP address of my ECS"
}

The label immediately after the output keyword is the name, which must be a valid identifier. The output block contains the following arguments:

  • value (mandatory): value of the output variable. Any valid expression is allowed as an output value.
  • description: describes the usage of an output variable.
    output "vpc_id" {
      description = "Check out the VPC ID"
    }
  • sensitive: marks output variables as sensitive and hides the output variable values on the CLI.
    output "vpc_id" {
      description = "Check out the VPC ID"
      sensitive   = true
    }
    
    $ terraform output
    vpc_id = <sensitive>

    Note: Output variables marked as sensitive are automatically hidden during output, but their output values can still display in the following ways:

    • The values of output variables are recorded in the state file and are visible to anyone who can access the file.
    • The sensitive output variable values in a child module can be invoked by its parent module and displayed on the CLI after being referenced by the related outputs and resources of the parent module.
  • depends_on: specifies the dependency of an output variable. Since output variables are only a means of exporting data, you do not need to set the dependencies between output variables and other resources or data.