Terraform: How to use Dynamic Blocks when Conditionally Deploying to Multiple Environments

Andrea Marinaro
Geek Culture
Published in
2 min readJul 15, 2021

--

Photo by Hello I’m Nik on Unsplash

In the previous publication I explained how to be able to implement a conditional logic when deploying resources with Terraform.

Accordingly to Terraform documentation, a dynamic block acts much like a for expression but produces nested blocks instead of a complex typed value.
It iterates over a given complex value and generates a nested block for each element of that complex value.
You can dynamically construct repeatable nested blocks like
setting using a special dynamic block type, which is supported inside resource, data, provider, and provisioner blocks.

But what if we need to implement a conditional logic outside the mentioned blocks?

Well, I thought it was not doable unless I tried to implement it with our Aws WAF code.

Currently, we have WAF in place within our organization for three environments: testing, acceptance and production.
After being hit by some massive bot scans we decided to implement rate limit WAF functions only on the external accessible PROD environment.

The tricky part here is that we have a generic WAF module that uses a web acl (aws_wafv2_web_acl) that is common across all the…

--

--