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 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 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
- 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.
- 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.
Thank you very much for sharing. Your article was very helpful for me to build a paper on gate.io. After reading your article, I think the idea is very good and the creative techniques are also very innovative. However, I have some different opinions, and I will continue to follow your reply.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good. https://www.binance.com/kz/register?ref=RQUR4BEO
After reading your article, it reminded me of some things about gate io that I studied before. The content is similar to yours, but your thinking is very special, which gave me a different idea. Thank you. But I still have some questions I want to ask you, I will always pay attention. Thanks.