All Products
Search
Document Center

Terraform:Output

Last Updated:Oct 15, 2025

In previous topics, we introduced resources, dependencies, and variables. In this topic, we will explore Terraform outputs.

What are outputs

Outputs are similar to return values in common programming languages. You can use outputs to view information about the infrastructure resources that you create in the command-line output.

Outputs have multiple purposes. The most common scenario is to print the properties of root module resources in the run log after you run the apply command. For example, most of the details of an ECS instance are calculated when you run the apply command and are available only after the instance is created.

In addition, outputs are used to pass information from one resource to another. For example, you can extract the value of a specific property of an ECS instance, such as its IP address, for another resource that requires this information, such as a security group rule.

Syntax for outputs

Outputs are declared in an output block. You can declare outputs anywhere in a Terraform configuration file. However, the best practice is to declare them in a separate file named outputs.tf.

image

The label after the output keyword is the name of the output. Similar to variable names, the name of an output must be unique within the current module. The output block can contain the following parameters:

  1. value

    This is a required parameter that specifies the value to be returned to the user of the module.

  2. description

    This is an optional parameter that provides a description of the purpose and expected value of the output. The description parameter is often used for documentation.

  3. sensitive

    This is another optional parameter that is used to mask the output. This parameter is useful when you need to hide confidential information, such as passwords or AccessKey secrets.

In the following example, we declare an output named vpc_id for the resource named my-vpc:

# Create a VPC
resource "alicloud_vpc" "my-vpc" {
  vpc_name   = "tf-vpc"
  cidr_block = "10.0.0.0/16"
}

output "vpc_id" {
  value = alicloud_vpc.my-vpc.id
}

After you run the terraform apply command, the ID of my-vpc is displayed in the output:

截屏2024-07-14 19.56.30.png

In addition, for resources that have been created, you can run the terraform output command at any time to query all outputs in the project.

Best practices for outputs

We recommend the following best practices for declaring outputs:

  1. Output only useful information

    Output only useful information, such as calculated information. Avoid outputting values that are repetitions of variable values or other known information. For example, for network resources, the following calculated properties are useful:

    1. id, which is the identifier of the resource

    2. router_id, which is the identifier of the router that is automatically created after the VPC is created

    3. route_table_id, which is the identifier of the route table that is automatically created after the VPC is created

  2. Provide meaningful names and descriptions, similar to variables

  3. Organize your code by placing all output values in a file named outputs.tf

  4. Mark sensitive outputs

    Do not try to manually encrypt sensitive values. Instead, rely on the built-in sensitive state management feature for support. When you declare outputs for sensitive values, make sure that these outputs are marked as sensitive. When an output is marked as sensitive, its value is masked in the output of the terraform plan or terraform apply command.