As you (probably) know, Terraform doesn’t support if statements.

Luckily we can achieve the same result by using a specific parameter called count.

You can think about it this way: you can set _count _to 1 on a specific resource and get one copy of that resource.

However, setting the same _count _parameter to 0 you won’t get any resource created.

So, as already mentioned, you won’t need if/else statements at all, Terraform use conditional expressions with the following format:

<CONDITION> ? <TRUE_VAL> <FALSE_VAL>

All that Terraform does is nothing but evaluating the boolean logic in CONDITION and, based on that, if the value is _true _it will return TRUE_VAL otherwise it’ll return FALSE_VAL if the result is false.

Just to give you some contest, we’re migrating our existing AWS infrastructure (Ansible&Boto3 deployed) in Terraform.

#cloud #terraform #infrastructure-as-code #cloud-computing #aws

Terraform: how to use conditionals for dynamic resources creation
1.35 GEEK