by_adr - Fotolia
Start using Terraform variables for IT resource deployment flexibility
Infrastructure-as-code adopters should try Terraform input and output variables as a more flexible way to define and save values for deployment configurations.
HashiCorp's Terraform enables IT engineers to write infrastructure configurations as code to both preview and deploy IT resources as quickly as application code. With declarative configuration statements, the infrastructure setup remains consistent, and IT teams can reproduce environments for test and deployment.
Terraform users describe these configurations -- for networking, domain name routing, CPU allotment and other components -- in resources, using the tool's configuration language. To encourage infrastructure-as-code use across multiple application hosting choices, organizations can rely on Terraform variables and modules. Variables are independent of modules and can be used in any Terraform resource. Variables inside a Terraform module, however, form something of a template that IT engineers can tweak according to the input they've assigned to the variables. For example, count can be a variable in an AWS module to deploy a certain number of VMs.
Types of Terraform variables
Terraform variables are a flexible and centralized way to define and save values, independent of any particular Terraform execution plan. If admins provide no values, default values can define variables in the plan. Teams can specify and override the default values through the command line.
Terraform variables fall into two categories depending on how they are used in a resource plan that an engineer wishes to execute.
Input variables. Define input variables with a variable keyword, followed by a name and then, in braces, a description, default value and type. There are different types of input variables: string, list, map and boolean.
String variables are a user-friendly approach that reduces complexity compared to using values directly in a Terraform plan.
String variables can be accessed later in a Terraform plan in double quotes as a string interpolation syntax. For example, rather than enter a given web API subscription key, the engineer includes the Subscription variable:
A list variable is used for a collection of values, where any value can be called by its respective index value. Basically, a Terraform list is a zero-indexed array of values, such as these values that designate development, quality assurance and production configurations:
List variables are implemented in the resource plan just like string variables but with index values.
Map variables comprise a collection of Key-value pairs, similar to a dictionary or hash table in a programming language. For example, a Terraform configuration can include a map of AWS instance type options in different sizes:
To access a corresponding value with a map variable, provide the key. For example, the key medium returns the value t2.micro:
A boolean input variable can only store true/false values.
Because of this true/false dichotomy, a boolean variable can be used in Terraform as a deciding factor to deploy a configuration step in an execution plan:
Output variables. When you successfully apply a Terraform configuration, the tool returns general information about the deployed resources and execution. In some deployments, the IT engineers want to capture more specific information, such as an IP address or VM state. Output variables can help. These variables are defined with a keyword output followed by a variable name, then a description and dependencies in braces.
Overall, Terraform variables provide a way to more easily and repeatedly make infrastructure changes in complex and diverse IT environments. Teams can tweak the values of variables that are stored in independent files to change an entire resource deployment plan without changing any piece of code.