Dynamic block allows to create nested multi level block structures in terraform code. Conditional usage of such blocks are really useful in many scenarios. For example, when we create Azure Cosmos DB, if we want to have read regions only in production environment, but only have one region setup for dev and QA environments we can, leverage the capabilities in Dynamic block of terraform. Let's explore with an example.
We need to get a read geo_location cerated for the Cosmos DB as shown in the commented code below in westus region. But we only need this additional region when the env is production and not in dev or QA. However, if we use the commented code it will always create additional geo_location. To achive creation of additional geo_location only in production we can use dynamic block instead of the commented code. Based on env validation condition dynamic block is set with a local variable (map with values).
The local variable is setup as shown below. The cosmosdb_regions_none_prod contains the map for one region where the cosmosdb_regions_prod containing two regions. Secondary region we can set via a variable as it may differ based on the deploying region and data residency policy requirements such as GDPR. Full example code is available in gitHub here.
No comments:
Post a Comment