Get startedGet started for free

Variables and Outputs

1. Variables and Outputs

Now let’s examine how you can use variables to parameterize a module, and outputs to pass resource attributes outside a module. The examples covered so far all had hardcoded resource attributes. For example, the name of the network is hardcoded in the main.tf file within the server module. If you use the server module more than once, you will receive name conflict errors when you execute terraform plan or terraform apply. In such scenarios, you must have the flexibility to configure a few attributes differently in different environments, and standardize them in others. With variables, you can customize aspects of modules without altering the source code. Variables are also useful when working with different environments such as development, production, and staging. It’s a best practice to run a small machine type in the staging environment and a larger machine type in the production environment. Let’s view an example of how to parameterize the name argument with input variables. The first step is to replace the hardcoded values within your module with a variable. Assign the argument that you want to parameterize – in this case network name – by using the format var. . This assignment provides the flexibility to configure the name argument when the module is called. Then declare the attributes that are parameterized in the variables.tf file. Place the declared variables in the same directory as the defined resource. Be sure to specify the appropriate variable type. Pass the value to the input variable when you call the module. Since you parameterized the network name, you can now reuse the same network module twice in the main configuration and provide a different name for each instance. Remember to run the terraform init command to download any modules referenced in a configuration. Unlike root configuration, you cannot pass values to variables at run time. To pass resource arguments from one module to another, the argument must be configured as an output value in Terraform. For example, the network module includes the network name. The server module should receive the network name from the module that defines the network. If configured in two separate modules, pass this value by using the output values. To pass the network name from the network module, declare the network name as the output value to expose the resource attribute outside of the module. Then define the network name as a variable in the server module, so that it can accept the values passed outside the module. Now, in the main configuration, reference the output value when calling the server module. You can refer to the output value by using the format module.module_name.output_value. In our example, we refer to the output value of the my_network module using module.my_network_1.network_name. Notice that both modules are called from the root configuration. Each time a module is instantiated, terraform init has to be executed. After you run terraform init, you can pass the network name from the network module to the server module.

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.