Best Way to create a nested loop?

Hi! I have a terraform module that creates OSP VMs with disks based on the parameters sent detertmining the amount of VMs and disks for each VM. For example, If I send the number 2 in the variables “volume_amount” and “amount” I create 2 VMS with 2 volumes each, named VM-1-Vol-[1,2] and VM-2-Vol-[1,2] Accordingly.
I have tried to do so using a “nested” loop using modulo but scaling it up in case I want to add VMs or new volumes doesn’t work and messes up the current volumes by deleting existing ones and recreating them.
Is there any good way to create and scale the configuration I described above? I’m using TF 0.12.

First: Upgrade. It might(?) not be strictly necessary to for this question, but the Terraform language went through major changes in 0.12 → 0.13. You’ll do yourself no favours waiting to cross this boundary, and if you have questions, you’ll find the collective knowledge of people who can help, is already on the wane, as this transition fades into the past.

Note that for existing Terraform state files, it is required to upgrade specifically from 0.12 to 0.13, before moving on to any later versions.

(The docs actually claim the same about 0.13 to 0.14, though in that case I believe it to be untrue - possibly copied forward from the previous release’s documentation without a true need.)

On to your question:

Since you have mentioned modulo, I guess that you are using the count attribute.

You should switch to using for_each so you can use string keys to identify each resource, rather than numbers, and avoid having to rely on attempting to pack multiple pieces of information into a single integer.

Here is an example I wrote recently showing how multiple Terraform for expressions can be used to create a map suitable for for_each using nested iteration: Multiple iteration in one resource - #2 by maxb

Hey, Thanks for the answer! I’ll try using for_each instead and see if could work for my use case.
In regards to 0.13, we tried uprading it but it didn’t go smoothly. Does it still work with on prem providers? we have an offline env we use it in.

Some things have changed in 0.13 and onwards about how providers are found and where you put them on disk to use them without reaching out to the network, but it’s still just as possible to use in an offline environment as before.

As for not going smoothly… being stuck on an end-of-life version forever more is not going to go smoothly either, in the longer term!

Yeah, that’s also true :slight_smile:
Could you point me to the docs regarding the location of offline providers in 0.13 onward?

The details are somewhat confusingly hidden away on the “CLI configuration file” page: CLI Configuration | Terraform by HashiCorp

But sometimes an example is more helpful than a reference doc:

    └── hashicorp
        ├── null
        │   └── 3.1.1
        │       └── linux_amd64
        │           └── terraform-provider-null_v3.1.1_x5
        └── vault
            └── 3.5.0
                └── linux_amd64
                    └── terraform-provider-vault_v3.5.0_x5

One important thing to know, is that with Terraform 0.13+, providers are now named using 3-part names:


When the FQDN is the official Terraform registry - - the first part (“”) is often omitted in configuration and display for humans - but still needs to be there in file paths on disk.