Terraform Associate Certification: Conditional Expression & Local values

Conditional Expression

If you have programmed in other languages such as C or Javascript you may know the ternary operator. Well, in Terraform you can use it as well and it is nothing but a conditional expression that uses the value of a boo…


This content originally appeared on DEV Community and was authored by Daniel Huerta

Conditional Expression

If you have programmed in other languages such as C or Javascript you may know the ternary operator. Well, in Terraform you can use it as well and it is nothing but a conditional expression that uses the value of a bool expression to select one of two values.

The syntax of it is as follows:

condition ? True_value : False_value

If condition is true, then the returned value will be True_value, otherwise will be False_value.

Let's see an example of this:

Screen Shot 2021-08-24 at 10.47.55

In this case, the amount of aws_instances will be created according to the value of the bool variable named istest. As you can see in the image, this value is set to false so only one aws_instance.prod will be created ?.

Local Values

A local value assigns a name to an expression, allowing it to be used multiple times within a module without repeating it.
Let's see an example to understand this better, in the next image you will see that I created a set of tags within a locals block.

Screen Shot 2021-08-24 at 10.57.19

I'm using these tags inside two different resource blocks but I'm not typing every tag on it, that's the advantage of the local values.

Local values support for expression

Local values can be used for multiple different use-cases like having a conditional expression.

Screen Shot 2021-08-24 at 10.58.47

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 future.


This content originally appeared on DEV Community and was authored by Daniel Huerta


Print Share Comment Cite Upload Translate Updates
APA

Daniel Huerta | Sciencx (2021-08-24T16:02:09+00:00) Terraform Associate Certification: Conditional Expression & Local values. Retrieved from https://www.scien.cx/2021/08/24/terraform-associate-certification-conditional-expression-local-values/

MLA
" » Terraform Associate Certification: Conditional Expression & Local values." Daniel Huerta | Sciencx - Tuesday August 24, 2021, https://www.scien.cx/2021/08/24/terraform-associate-certification-conditional-expression-local-values/
HARVARD
Daniel Huerta | Sciencx Tuesday August 24, 2021 » Terraform Associate Certification: Conditional Expression & Local values., viewed ,<https://www.scien.cx/2021/08/24/terraform-associate-certification-conditional-expression-local-values/>
VANCOUVER
Daniel Huerta | Sciencx - » Terraform Associate Certification: Conditional Expression & Local values. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/24/terraform-associate-certification-conditional-expression-local-values/
CHICAGO
" » Terraform Associate Certification: Conditional Expression & Local values." Daniel Huerta | Sciencx - Accessed . https://www.scien.cx/2021/08/24/terraform-associate-certification-conditional-expression-local-values/
IEEE
" » Terraform Associate Certification: Conditional Expression & Local values." Daniel Huerta | Sciencx [Online]. Available: https://www.scien.cx/2021/08/24/terraform-associate-certification-conditional-expression-local-values/. [Accessed: ]
rf:citation
» Terraform Associate Certification: Conditional Expression & Local values | Daniel Huerta | Sciencx | https://www.scien.cx/2021/08/24/terraform-associate-certification-conditional-expression-local-values/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.