Solving the problem of different terraform URLs in the source code

Sam A
2 min readSep 8, 2022

--

If you have multiple terraform Enterprise environment with different hostnames this is how you can fix the problem and avoid changing the URL in the source code by adding a generic URL which can be resolved at the run time.

Lets say you have two Terraform URLs

  1. dev.terraform.com
  2. prod.terraform.com

In your terraform code each time you want to point to the registry in the dev you need to change the source code to point to that hostname and each time you need to access the registry in the prod you need to change it to the prod hostname

#main.tf for devmodule "test-resourcegroup"{
source = "dev.terraform.com/terraform_registry/test-resourcegroup/azurerm
version = 10.0.2
...}#main.tf for prodmodule "test-resourcegroup"{
source = "prod.terraform.com/terraform_registry/test-resourcegroup/azurerm
version = 10.0.2
...}

wouldn’t it better if there was an easier way ?

So terraform can use a generic URL that can be changed at the run time. Your ENV specific URL can be changed to “localterraform.com” as shown in example below :

#main.tf for prod and devmodule "test-resourcegroup"{
source = "localterraform.com/terraform_registry/test-resourcegroup/azurerm
version = 10.0.2
...}

Also for this work when you run terraform from terraform cli you need to set these ENV variables at the run time and also add these to the .terraformrc

#ENVs
tfe_module_regsitry_dns=dev.terraform.com
cli_config_file_name=<home-dir>/.terraformrc

The content of terraformrc file

host "localterraform.com"{
services = {
"module.v1" = "https://dev.terraform.com/api/registry/v1/modules"
"tfe.v2" = "https://dev.terraform.com/api/v2"
}}
credentials "localterraform.com" {
token = "<TOKEN>"}

--

--

Sam A

Senior DevOps Consultant, a tech enthusiast and cloud automation expert that helps companies improve efficiency by incorporating automation