Specify Source Module directory

I source a git module as below in my Terraform 0.12 code

module "autospotting" {
  source                                    = "github.com/autospotting/terraform-aws-autospotting?ref=0.1.1"
  ...
  ...
}

At the time of terraform initialization, the below directory structure gets created. Is there a way to move this into a another directory and specify that with terraform init ?

Hi @vikas027,

The .terraform directory is where Terraform caches some items that need to be retrieved during terraform init so that they can be used in other Terraform commands. Any module or provider that is being installed from a remote system rather than from the local filesystem will always have its local copy cached under .terraform, and there is no mechanism to change that. The .terraform directory is not intended to be included in your version control, so it’s common to put .terraform/* in your .gitignore, if you’re using Git.

If your goal is to have a local copy of the module in your own repository so that you are not dependent on an external repository, the best way to do that is to clone the repository for the module in question directly with git clone and then copy the code from there to the location where you want to keep it in your own repository. Then you can specify that chosen directory using a relative path, rather than a git source:

module "autospotting" {
  source = "./modules/autospotting"
}

The standard module structure calls for putting nested modules (those that don’t live in their own separate repositories) in directories under a directory called modules, which is the convention I followed in the above example. However, you can choose whatever directory structure works for you, as long as the path you give starts with either ./ or ../ to tell Terraform to treat it as a local directory path rather than as a remote source.

2 Likes

Thank you, @apparentlymart. That helps.