Terraform: Understanding Variables

Share At:

Introducing the Terraform Provider for Lacework - Lacework

Variables in Terraform are a great way to define centrally controlled reusable values. The information in Terraform variables is saved independently from the deployment plans, which makes the values easy to read and edit from a single file.

Terraform supports a few different variable formats. Depending on the usage, the variables are generally divided into inputs and outputs.

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.

Output variables, in contrast, are used to get information about the infrastructure after deployment. These can be useful for passing on information such as IP addresses for connecting to the server.

Input variables

Input variables are usually defined by stating a name, type and a default value. However, the type and default values are not strictly necessary. Terraform can deduct the type of the variable from the default or input value.

Variables can be predetermined in a file or included in the command-line options.

Below is a sample variable file called variables.tf

Output variables

Output variables provide a convenient way to get useful information about your infrastructure. Much of the server details are calculated at deployment and only become available afterwards. Using output variables you can extract any server-specific values including the calculated details.

For example, you could have Terraform show your server’s IP address after deployment with the output variable below.

Note that the place of the public network interface on the list of network interfaces depends on which the order the NICs are defined in the resources.

Example: Simple Demonstration on how to define variables and use them

  1. Let’s change to the directory where our terraform configuration file exists. You can see there are 2 files called main.tf and variables.tf.
  2. Let’s explore them

3. Now initialize the terraform working directory containing configuration files.

4. Now Let’s create the execution plan using terraform plan

5. Now lets execute the plan using terraform apply command.

6. Now you can see that we have a file called first-jedi which contains the content we had defined in the variables.tf file.

This concludes our tutorial for Terraform Variables.

Happy Learning !!!


Share At:
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Back To Top

Contact Us