Working with input variables in Terraform
- we saw earlier how to work with input variables in Terraform
- in this lecture, we will take an in-depth look to them
How to declare an input variable in Terraform?
- we saw previously that we can declare an input variables using a variable block, so th variable keyword here tells us that this block is used to declare an input variable
- the label after the variable keyword is the name given to the variable. this name will be used later to assign a value to the variable from the outside and to reference the variable's value when needed
- and inside the variable block, we can assign a value to the variable using the default keyword
- here is an example
- in the variables.tf file, we have declared two variables, one called filename and the other is called content
- we have assigned the value "./file.txt" to the filename argument and the value "Welcome to Terraform!" to the content variable
- now these values can be accessed to set arguments in a resource block and we can reference them as attributes of an object named var
- for example here we have set the filename argument of the local file resource using the filename variable and the content argument using the content variable
- now, when we apply this configuration file, a file called file.txt will be created with the content "Welcome to Terraform!" inside it
Simple types in Terraform
- Let's learn now about variable types in Terraform
- Terraform supports a set of simple types such as
- string: here the value will be a sequence of Unicode characters representing some text such as "Welcome to Terraform", "server1", "dev",
- number: the variable here will accept any numeric value. The number type can represent both whole numbers like 15, 100 and fractional values 12.5, 1.65
- bool: here the value can be either true or false
- if we don't specify the any of these types or we set the type to any, the type of the variable will be any, so the variable will accept any value
Setting the type of a variable
- the type of a variable can set using the type argument in the variable block.
- for here here in the variables.tf file we have declared:
* a variable called location. its type is a string
* a variable called count which will accept any numeric value such as 5, 12.3
* a variable called is_sensitive. the type here is bool, so only true or false will be accepted
* and finally for the variable called env, we didn't specify the type, that's why any value will be accepted
Variable block arguments
- in addition to the default and type arguments, there are some other arguments that we can set while declaring a variable. we can set
* the description argument: to give more information for what this variable is used for
* nullable: The nullable argument in a variable block controls whether this variable can accept the value null
* sensitive: Setting a variable as sensitive prevents Terraform from showing its value in the plan or apply output, when you use that variable elsewhere in your configuration.
* validation: which is a block that we can use in a variable blocks to add more restrictions on the values that can be assigned to a variable and to specify custom validation rules for the variable. here for example, the length of the string given to the content variable should be greater than 4 otherwise you will get an error telling the given value is invalid
You have to know that all these arguments and an input variable can be declared without setting any arguments
You can learn more about input variables in Terraform form this video
Comments
Post a Comment