Hi,
i have got two environments, where i want to run the terraform project. The first one is the dev environment, the second one is the stage and production environment). In my szenario i have to create different resources (in azure), as an example think of staticly provided ip addresses.
My idea was to create these resources based on an condition (either an environment variable or based on the azure tenant id). But as I figured out there is no option for these conditions, only to fill variables like a name, a region or something else based on a condition?
Would be very helpful if you could summerize the suggested behavior for that case.
Kind regards
Hi @scs-tec,
You can conditionally create the values in a resource’s for_each
expression, which will correspondingly create instances based on those values. If the for_each
expression evaluates to an empty map or set, then no instances will be created.
1 Like
I am in a similar situation (just with my own provider and resources). Using for_each
seems to be very verbose. Especially considering that I am using modules, I will need to pass these inputs down to into the submodules.
After reading How to create reusable infrastructure with Terraform modules | by Yevgeniy Brikman | Gruntwork it seems like it is a better idea to write modules that get published (in my case via GitLab) and then reuse them inside a multi-provider root module. This approach seems to be a lot of work however and very unintuitive for novice Terraformers.
I would very much appreciate it if @scs-tec could share what was the final chosen solution.