Output Values
1. Output Values
We’ve covered resources, meta-arguments, dependencies, and variables. Let's wrap up by exploring output values. Output values are similar to return values in common programming languages. With outputs, you can view information about the infrastructure resources you created on the command line. Output values are used for several purposes. The most common use case is to print root module resource attributes in the CLI after its deployment. Most of the server details are calculated at deployment and can only be inferred post-creation. Output values are also used to pass information generated by one resource to another. For example, you can extract server-specific values – such as an IP address – to another resource that requires this information. Output values are declared using the output block. The keyword ‘output’ indicates that the label associated with the keyword is the name of the output value. You can declare an output value anywhere within a configuration, but the recommended best practice is to declare them in a separate file named output.tf. The arguments that can be included within an output block are: value, which is a required argument that returns a value to the user of the module. Description, which is an optional argument used to provide an explanation of the purpose of the output and the value expected. The description is often used for documentation purposes. And sensitive, which is another optional argument used to mask the value to a resource attribute. This argument is useful to hide the accidental display of a resource attribute that is meant to be confidential, such as password information. In this example, we have declared an output value for the object named picture. After the object is successfully uploaded, the object URL is displayed on the screen. We recommend that you use output values, instead of user-supplied inputs, for the computed attributes of a resource. You can query all output values used in a project by running the terraform output command. Let’s explore best practices for output values. Only output useful information, such as computed information. Avoid outputting values that simply regurgitate variables or provide known information. For example, for a network resource, the following computed attributes are useful: id, which is an identifier for the resource Gateway_ipv4, which is the gateway address for default routing out of the network. And self_link - the URI of the created resource. As with variables, provide meaningful names and descriptions. Organize your code to include all output values in a file named output.tf. Finally, mark sensitive outputs. Instead of attempting to manually encrypt sensitive values, rely on built-in support for sensitive state management. When exporting sensitive values to output, make sure that the values are marked as sensitive. When a value is marked sensitive, the data is masked from the output of terraform plan or terraform apply command.2. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.