Get startedGet started for free

Variables

1. Variables

So far in this module you’ve been hardcoding resource argument values, but what if you want to parameterize your configuration, and define argument values during the apply phase of the Terraform workflow? Perhaps you want to standardize your code but maintain the flexibility to customize resource attributes at runtime. This is where variables come in. With variables, you can parameterize values shared between resources. Input variables serve as parameters for Terraform, allowing easy customization and sharing without having to alter the source code. After a variable is defined, there are different ways to set its values at runtime, including environment variables, CLI options, and key-value files. In the code shown in the example, the name, location, and storage class are hardcoded. You can declare any of these attributes as a variable, and specify the details at run time. Variables separate source code from value assignments. Let’s explore how to declare an input variable. Variables must be declared in the variable block. It’s recommended that you save all variable declarations within a separate file named variables.tf. The label next to the keyword variable gives the variable a name. There are two rules for naming variables. First, the name of the variable must be unique within a module. Second, variable names cannot be keywords. There are no required arguments for a variable, so a variable block can be empty. Terraform can automatically deduce the type and default values. The type argument specifies value types that are accepted for the variable. Terraform supports the following primitive variable types: Bool, which is used for binary values such as true or false without the quotes. Number, which is used for numeric variables. And string, which is used for a sequence of unicode characters. Default is another meta argument, used to assign a default value to an attribute. To access the value of a variable declared within the module, you can use the expressions var. . In the example on the slide, the variable bucket_storage_class is formatted as var.bucket_storage_class in the resource block. The name of the variable in the variable block has to match the reference made within the resource block. The default value can be overridden by assigning a value in environment values, or . tfvars files or -var option. The folder structure shows that the variable declaration is written within the variables.tf file, which is a recommended best practice. The variable is assigned to the storage_class argument. The default value in quotes is used when creating the resource. The description documents the purpose of the variable. The description is displayed during the apply phase when the variable doesn't have a defined value. The description should explain the purpose of the variable and the expected value. The description string is often included in documentation, so it should be written from the perspective of the user rather than its maintainer. Comments can be used by the maintainer. Sensitive, as the name suggests, is a variable argument used to protect sensitive information from being displayed in command outputs or log files. The acceptable value for sensitive is true. When set to true, the value is marked sensitive in the output of terraform plan or terraform apply. This argument is beneficial when dealing with information such as database credentials or API tokens. Marking variables as sensitive will eliminate the accidental exposure of confidential information. In this example, the user_information variable is marked sensitive. The resource foo uses name and address, which are both sensitive variables. When terraform plan or apply is run, values are not displayed because they are marked as sensitive. There are several ways to set variable values at run time. First, you can use . tfvars files to quickly switch between and version sets of variables. You can also use CLI options. CLI options are useful for running quick examples on simple files. Environment variables are useful in scripts and pipelines. If a required variable has not been set by using one of the described methods, you can use the CLI prompt When you have many variable definitions to input, providing the value in the command line might not be feasible. Instead, specify the variables in a definitions file with either a . tfvars or . tfvars.json extension. Variable definitions follow the same syntax as HCL, but include only variable name assignments. Terraform automatically loads the variable definitions files as long as they are exactly named terraform.tfvars, terraform.tfvars.json, . auto.tfvars, or . auto.tfvars.json. The definition provided in the . tfvars file overrides the definition in the default argument and environment variable. If you want to specify the value of a variable individually on the command line, you can use the -var option. These values can be entered when running either terraform plan or terraform apply. This method is frequently used for automation, where the -var is sourced from another environment variable. It’s also useful when running quick examples on simple files. If you use a . tf extension, you must specify the filename using the -var-file option on the command line. Set variables can be overridden at deployment. You can reuse the variable file and still customize the configuration at deployment. The -var option takes the highest precedence over all other variable assignment methods. If the variable value is assigned using multiple methods, the value defined using the -var option is the value that is assigned to the variable. If a required variable has not been set using a described method, Terraform prompts you on the CLI. In this example, the variable is not parameterized and has not been assigned a value. Therefore the CLI prompts you to enter a value during the plan phase. You can also validate the value assigned to a variable by including a validation subblock with the variable block. The validation block includes a condition argument for which the validation rule is assigned. In this example, contains is used as the condition argument, and validates that the storage class value is upper case and an accepted value.

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.