Terrafrom remote .tf storage

Hey there
we have gitlab where main.tf is stored, if someone wants to create vm, they need to pull main.tf then apply, then push it back with the changes.
Is there a way to store main.tf somewhere remotely, like consul maybe, so that you dont need to pull and push it all the time?

You can store your code files wherever you want, such as NFS or other filesystems, but the big advantage you get from storing code in git is the versioning & workflow you get from it - you know who & why changes were made, and can implement things like code reviews via PRs.

I understand it can be stored anywhere, but can terraform fetch it remotely?

Terraform expects that you’ll obtain the root module code before running Terraform, so there’s no functionality in Terraform specifically intended to meet your use-case.

However, there is a feature that is intended to make it easier to get started with modifying example modules in the Terraform Registry: terraform init -from-module=... takes an argument in the same syntax you’d use in the source argument of a module block (see Module Sources) and fetches the specified module’s source code into the current working directory.

Although it’s not really intended for what you are trying to do, if you can put the source code in a location that Terraform can install modules from and then use -from-module to fetch it:

terraform init -from-module='s3::https://s3-eu-west-1.amazonaws.com/examplecorp-terraform-modules/vpc.zip'

Terraform doesn’t support installing modules from Consul in particular, because Consul’s key/value store isn’t really intended for distributing packages of files, but there are various other available options.

I’m not really sure that this is really a significant improvement over cloning from git in the normal way, though. It does save running git clone ... but you still need to do all of the other steps, and in particular Terraform doesn’t have any analog to git push so this wouldn’t be appropriate for any situation where you intend to modify the module code.

Depending on your goals here you could also consider Terraform Cloud, which has the option to integrate directly with a Git repository and automatically start a Terraform run each time you commit to the main branch of that repository.