In Terraform, variables are an excellent way to define centrally managed reusable values. Terraform variables are saved separately from deployment plans, making it simple to view and edit the values from a single file.
In this blog post, we'll cover types of variable in Terraform and how to define them in Terraform configuration.
Types of Variables -
1. Local Variables
Local variables are declared using the locals block. It is a group of key-value pairs that can be used in the configuration. The values can be hard-coded or be a reference to another variable or resource.
Local variables are accessible within the module/configuration where they are declared. Usage of local variables is similar to data sources. However, they have a completely different purpose. Data sources fetch valid values from the cloud provider based on the query filters we provide. Whereas we can set our desired values in local variables — they are not dependent on the cloud providers.
Usage of local variables is similar to data sources. However, they have a completely different purpose. Data sources fetch valid values from the cloud provider based on the query filters we provide. Whereas we can set our desired values in local variables — they are not dependent on the cloud providers.
As a best practice, try to keep the number of local variables to a minimum. Using many local variables can make the code hard to read.
2. Input Variables
The input variables are used to define values that configure your infrastructure. These values can be used again and again without having to remember their every occurrence in the event it needs to be updated.
Input variables are similar to local variables. They are used to assign dynamic values to resource attributes. However, the difference is in the way they are used. Input variables allow you to pass values before the code execution.
Further, the main function of the input variables is to act as inputs to modules. It let you customize aspects of Terraform modules without altering the module's own source code. This functionality allows you to share modules across different Terraform configurations, making your module composable and reusable.
Arguments -
Terraform CLI defines the following optional arguments for variable declarations:
default - A default value which then makes the variable optional.
type - This argument specifies what value types are accepted for the variable.
description - This specifies the input variable's documentation.
validation - A block to define validation rules, usually in addition to type constraints.
sensitive - Limits Terraform UI output when the variable is used in configuration.
nullable - Specify if the variable can be null within the module.
Input variables support multiple data types. They are broadly categorized as simple and complex. String, number, bool are simple data types, whereas list, map, tuple, object, and set are complex data types.
3. Output Variables -
Output variables provide a convenient way to get useful information about your infrastructure. Output values are similar to return values in programming languages.
Output variables in Terraform are used to display the required information in the console output after a successful application of configuration for the root module. Each output value exported by a module must be declared using an output block:
output "instance_ip_addr" {
value = aws_instance.server.private_ip
}
The label immediately after the output keyword is the name, which must be a valid identifier. In a root module, this name is displayed to the user; in a child module, it can be used to access the output's value.
The value argument takes an expression whose result is to be returned to the user. In this example, the expression refers to the private_ip attribute exposed by an aws_instance resource.
For more information on defining these different types of variables, please refer this documentation. It's important to note that Terraform does not allow the usage of variables in provider configuration blocks. This is mainly to adhere to best practices of using Terraform.
I hope you find this blog helpful in understanding Terraform variables.
Thank you for reading!
*** Explore | Share | Grow ***
Commentaires