• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


26 Mar, 2025

Updated at 20 May, 2025

Do I understand terraform wrong?

I have envisioned the following directory structure for my terraform installation, but cannot piece those directories together. I am now at a stage where I think, that maybe I got terraform completely wrong and do not understand the concept.

vsphere-datacenter/
├── modules/               # contains resources that are common to all
│   ├─ compute_clusters.tf # contains the clusters ("vsphere_compute_cluster")
│   ├─ datastores.tf       # contains the datastores ("vsphere_datastore")
│   ├─ resource_pools.tf   # contains the resource pools ("vsphere_resource_pool")
│   └─ networks.tf         # contains the networks ("vsphere_network")
│
├── resources/
│   ├─ test                # contains the configuration for the test environment VMs
│   │  ├─ windows/         # contains the configuration for all windows test env VMs
│   │  │  ├─ main.tf       # defines stuff that is common for all windows test VMs
│   │  │  ├─ main.tfvars   # defines variable values that are common for all windows test VMs
│   │  │  ├─ vars.tf       # variable definition
│   │  │  ├─ windows-test-vm1.tf.json # one file per VM ...
│   │  │  ├─ windows-test-vm2.tf.json
│   │  │  ...
│   │  └─ linux/
│   │     ├─ main.tf       # defines stuff that is common for all linux test VMs
│   │     ├─ main.tfvars   # defines variable values that are common for all linux test VMs
│   │     ├─ vars.tf       # variable definition
│   │     ├─ linux-test-vm1.tf.json # one file per VM ...
│   │     ├─ linux-test-vm2.tf.json
│   │     ...
│   ├─ prod/
│   │  ├─ windows/
│   │  │  ├─ ...
│   │  └─ linux/
│   │     ├─ ...
│   └─ ...
├── main.tf                # contains all common info, e.g. provider, vsphere_datacenter, ...
├── main.tfvars            # variable values, such as vsphere_server, ...
└── vars.tf                # variable definitions (vsphere_password as sensitive, etc., ...)

What I want to achieve, is that in order to deploy, I just create a new some-test-vm3.tf.json file in the respective resource directory that contains the information on how the new VM should be configured and just run terraform apply from that directory, maybe enrich it with templates for multiple disks, ...

But from what I found so far, that does not seem to be how terraform "thinks".

I tried using "module" to include the subdirectories from my vsphere-datacenter/ directory, but get "undeclared resource" errors for the things I have defined outside of the subdirectory.

I cannot imagine that everyone simply creates a huge "VMs.tf" file and puts each and every VM into that file - or create one file per VM in the same directory?! So how do others handle this?

Any help is greatly appreciated.