Conditional Expressions/Fxns Flashcards
What are conditional expressions in terraform?
They allow you to choose between two values based on a condition
Whats the syntax of conditional expressions in Terraform?
condition ? true_val : false_val
Ex: instance_type = var.environment == “dev” ? “t2.micro” : “m5.large”
What conditions can you use in Terraform?
== Equal to
!= Not Equal to
&& - Both conditions have to be true
Ex: instance_type = var.environment == “prod” && var.region == “us-east-1” ? m5.large : t2.micro
What is passed to the lookup function in Terraform and what does it expect?
The lookup () function requires that you first pass a map variable followed by a key.
Ex. ami = lookup (var.ami, var.region)
*where var.ami is a map variable with key/value pairs and var.region is the key passed.
If a key passed to a lookup function doesn’t match a value in the map variable, what does the lookup function return
The default value
How does the length function work in Terraform?
It determines the length of a given list, map, or string
Ex:
length ([“first”,”second”]
Answer = 2
> length({“a” = “b”})
1
length(“hello”)
5
How does the element function work?
The element function retrieves a single element from the list
Ex:
> element([“a”, “b”, “c”], 1)
b, since the first element in a list is 0
How does the formatdate () function work?
How does the timestamp() function work?
It returns a UTC timestamp string in RFC 3339 format
Ex
> timestamp()
2018-05-13T07:44:12Z