Terraform Conditional logic and dynamic configuration blocks

Jack Roper
CodeX
Published in
4 min readJul 28, 2021

--

Photo by Joshua Aragon on Unsplash

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…

--

--

Jack Roper
CodeX

A blog about DevOps & Cloud Tech. Specializing in Terraform, Kubernetes, Azure & Azure DevOps! ☁️