القائمة الرئيسية

الصفحات

Input variables in Terraform

 What are Input variables in Terraform?


- we will learn in this lecture how to work with input variables in terraform


1. Why we need input variables in Terraform?


-but, before that let's revise how to create a very simple resource with terraform such as a local file


-to create a file with terraform, we use a block of type resource and we set the type of the block using the resource keyword


- and then we tell terraform that we want to create a file locally using the local_file type, so here local is the name of the provider and file is the resource that we want to create and then we give our resource a name, which "my_file"


- inside the block, we set the arguments used for this resource, we give the value "./file.txt" to the filename argument and the string "hi, how are you?" to the content argument


- and after creating the file, we execute the terraform init command to initialize the providers and then the terraform plan command to generate the execution plan and finally the terraform apply command to apply the execution plan


- in this example, the values given to the arguments are hardcoded in the configuration file 

- it would be better if we can store this values in a separate location so they can be used when we create other resources and instead of changing them in different places, we change them in a central place


-  This flexible approach will allow us also, to utilize the same configuration file across multiple environments or with different settings simply by changing the assigned values to the variables.


- here we work with simple data. but when we work with sensitive data, it’s recommended that you do not hardcode specific values, such as API keys and passwords, directly into your configurations. Instead, you should treat them as variables that can be dynamically provided when needed.


- all of that is achieved in terraform using input variables


- so instead of hardcoding the values in the configuration file, we can store them using input variables


3. How to use input variables in Terraform?


- to declare an input variable, we add a block of type variable, so here the variable keyword is used to set the type of the block. 


this is followed by a label which is the name given to the variable, and then to store a value in this variable, we can use the default argument followed by the value that we want to store in this variable.

  

- variables can be declared in the same terraform file with the resource blocks, but as a convention, we declare them in a separate file called variables.tf


- so let's add a file here and called it variables.tf where we will declare two input variables one for the file name and another for the content


-now, to make use of the values stored in a variable we use var. the variable name. for example to use the value stored in the filename variable, we set the filename argument = var.filename and the same for the content argument

Comments