Hello,
I’m new to Terraform and we had a solution provided to us by a vendor. But not quite happy with the all hardcoded stuff, copy of copy of copy … Lots of typing.
I’ll explain what is my goal.
I want to create my Infrastructure for a BI environment with TerraForm and a Azure environment. I have 9 subscriptions, a INT (Internal Facing), EXT (external facing) and RES (Internal, but restricted). These 3 are found in another 3, DEV,UAT,PRD.
As not all services exists in all 9 I thought coming up with 9 configs, and 1 major ‘execution’ module-root. (this is my terminology)
This module checks for example if in the running environment (1of9) if DataFactories have to be created. If so he should launch all .tf in a given directory. Why not 1, well I thought that it would be easier just to copy a file from the DEV structure to the UAT than to edit each time a file in UAT and ad a part of script.
But my first bump wat that I couldn’t user variables in the source environment. So before running into quite some yes/no possible I would like to inform if my vision/thoughts are doable and if possible a little hint/help into the right way …
I’m interpreting your question as something along the lines of “I need to be able to express more conditional and logic stuff in my Terraform statement”. I think a slight change of mindset might be in order, because this is a typical shift that I too have had to undergo.
While your internal train of thought might go something like:
if (the environment) is (dev in external)
deploy external resources for dev environment
else (all the other scenarios)
end
Terraform however doesn’t filter in this procedural way. There are two ways to decide if and how many of a particular resource needs to be created, both done with count.
if you are deciding whether a resource is needed, use a conditional count
if you need more than one of the same resource to be created, use a for_each
There are tutorials for how to use these meta arguments:
If you find HCL a bit too strange to wrap your head around, it may be worthwhile looking at using the Terraform Cloud Development Kit, which will allow you to write in a more familiar language other than HCL.
HCL is a description language not a programming language. It has some logic embedded but you can’t compare it to a programming language. Depending on where you come from, adding another tool might help express what you want ultimately. One way of doing this is using CDKTF or Pulumi. But if you are not a developper, this might be a massive undertaking.