How to use init with modules and subfolder structure

Hey Terraform community,

As I’m new to terraform and we are still figuring out how to structure our infrastructure code, there is a question i didn’t find an answer to.

We are using the following directory structure for our code:

environment
—test
-----subgroup(describes part of Test stage)
--------terraform.tfvars (declaring values to use in modules)
--------terraform.tf (referencing our modules)
—prod
-----subgroup
-------…
modules
—modulename
-----modulename.tf
-----input.tf (for variable definition)
-----output.tf (output definition

All of this is planned to use a remote state in an Azure Storage Account.

Our question is: where and how do we use “terraform init” in this setup. Because my understanding was, that its run once in a directory where the terraform.tf file is (like in the environments subdirectories).

Is there any other way to not initialize each subdirectory for using our modules? Or did i got the whole structure wrong?

Greetings Fabian

1 Like

Hello @fajanssen,

You are correct! In your setup, you will need to run terraform init in each working subdirectory (i.e., cd environment/test/subgroup && terraform init) . The structure you outlined is a common pattern for Terraform VCS repository structure.

There’s a Github thread with a useful discussion on this, as it’s a commonly cited point of friction. We’ll likely revisit it in the future!

2 Likes