Terraform Conditional logic and dynamic configuration blocks
Introduction
Conditional logic can be used for scenarios when you will need to define parts of a Terraform configuration for a particular resource only when needed.
Also when some resource types include repeatable nested blocks in their arguments, these will need to be handled in your module dynamically. To dynamically construct repeatable nested blocks you can use the dynamic block type. Dynamic blocks reduce the amount of code needed in the module and generate multiple blocks as needed.
Azure Application Gateway Example
For example, consider an Azure Application Gateway. The capacity type can either be specified as Manual:
Or Autoscale:
As you can see when autoscale is selected, different options are required. The manual option requires the instance count to be set, whereas the autoscale option requires the minimum and maximum instance counts to be…