Variables in Terraform
Variables in Terraform
To keep the things dynamic in terraform configuration and not hardcoding the values, we can
make use of variables.
We can define variables in a separate file and refer them to the code.
Approaches of variable Assignment
• Variables can be defined and mentioned in a number of ways.
• Default file is variable.tf (To define variables and default values)
• To specify explicit values, we have another file called terraform.tfvars
• terraform.tfvars is the default file name, if we have a custom file name, we can mention
it with command
variable "region-name" {
default = "us-east-1"
type = string
}
Here region-name variable can only accept string data type and if provided with any other data
type, it will throw an error.
Variable data types supported by terraform
Terraform supports a variety of data types like string, map, number, bool, list tuple.
To read in detail, please follow the below link
1. string
2. map
3. number
4. bool
5. list
6. any
7. tuple
CONDITIONAL EXPRESSIONS
A conditional expression uses the value of a bool expression to select one of two values.
Syntax of Conditional expression:
condition ? true_val : false_val
If the condition is true then the result is true_val. If the condition is false then the result is
false_val.
Let’s assume that there are two resource blocks as part of Terraform configuration.
Depending on the variable value, one of the resource blocks will run.
Locals
A local value assigns a name to an expression, allowing it to be used multiple times within a
module without repeating it.
• Local values can be helpful to avoid repeating the same values or expressions multiple
times in a configuration.
• If overused they can also make a configuration hard to read by future maintainers by
hiding the actual values used
• Use local values only in moderation, in situations where a single value or result is used in
many places and that value is likely to be changed in the future.
TERRAFORM FUNCTIONS
• The Terraform language includes a number of built-in functions that you can use to
transform and combine values.
• The general syntax for function calls is a function name followed by comma-separated
arguments in parentheses:
function (argument1, argument2)
Example:
> max(5, 12, 9)
12
The Terraform language does not support user-defined functions, and so only the functions
built into the language are available for use.
• Numeric
• String
• Collection
• Encoding
• Filesystem
• Date and Time
• Hash and Crypto
• IP Network
• Type Conversion
There is practically a large number of functions that terraform supports, to check how these
work, we can make use of terraform console command and test them out.
Ex
Using loops in terraform
type = list(string)
count = "3"
vpc_id = aws_vpc.main.id
cidr_block = var.subnet_cidrs[count.index]
tags = {
Terraform Datasource
Using data source we can fetch certain information from aws account dynamically
Terraform Import
Let's say there is a resource manually created and we want that resource to be managed by
Terraform Workspace
Using terraform workspace we can manage multiple environments with the same configuration