This tutorial points terraform init at a Virtual Registry that caches providers from registry.terraform.io, and then from a JFrog Artifactory Terraform repository.
virtual_registry:
registries:
- name: terraform
remotes:
- url: https://registry.terraform.io
The examples below reach the registry as https://terraform.example.com, a host that the registry named terraform answers (see name).
The registry serves the version lists and download descriptions, and a provider’s files come from another host: releases.hashicorp.com for HashiCorp’s providers, and GitHub for most others. The registry rewrites the links in each download description so that the files come through it too. With deny_unknown_hosts set, those hosts have to be listed, along with the host GitHub redirects release downloads to:
virtual_registry:
redirects:
deny_unknown_hosts: true
extra_known_hosts:
- releases.hashicorp.com
- github.com
- release-assets.githubusercontent.com
Package types describes what is cached and for how long.
There are two ways. Name the registry in each provider’s source, or send every provider from registry.terraform.io to it without changing the configuration.
A provider’s source starts with the hostname of the registry it comes from. Put the registry’s hostname there:
terraform {
required_providers {
aws = {
source = "terraform.example.com/hashicorp/aws"
version = "~> 5.0"
}
}
}
Terraform records the provider under that address, terraform.example.com/hashicorp/aws, in .terraform.lock.hcl and in the state. A configuration that already uses hashicorp/aws sees it as a different provider, so this suits new configurations.
A host block in the Terraform CLI configuration tells Terraform where registry.terraform.io serves providers. Point it at the registry, in ~/.terraformrc, or in a file that TF_CLI_CONFIG_FILE names:
host "registry.terraform.io" {
services = {
"providers.v1" = "https://terraform.example.com/v1/providers/"
"modules.v1" = "https://registry.terraform.io/v1/modules/"
}
}
The block replaces every service the host offers, not only the ones it lists, so modules.v1 keeps modules coming from registry.terraform.io. Without it, a configuration that uses a module from there fails with Error accessing remote module registry. The Virtual Registry does not cache modules.
Sources stay as they are, hashicorp/aws, and so do the provider addresses in lock files and state. Providers download through the registry from the next terraform init:
terraform init
The registry can front an Artifactory instance that has a Terraform repository, instead of registry.terraform.io:
virtual_registry:
registries:
- name: terraform
remotes:
- url: https://artifactory.example.com
Artifactory serves its Terraform repositories at /artifactory/api/terraform/, and the registry passes those paths through. Clients reach it in one of two ways, the same as they reach Artifactory itself.
As a provider registry, with the repository key folded into the namespace of each source, in the form <host>/<repo>-rt-ns-<namespace>/<type>. For a repository named terraform-remote:
terraform {
required_providers {
aws = {
source = "terraform.example.com/terraform-remote-rt-ns-hashicorp/aws"
version = "~> 5.0"
}
}
}
Or as a network mirror, which keeps sources as they are. In the CLI configuration:
provider_installation {
network_mirror {
url = "https://terraform.example.com/artifactory/api/terraform/terraform-remote/providers/"
}
}
Terraform does not check a provider’s signature when it installs from a network mirror, and reports it as unauthenticated. Use the provider registry form where the signature matters.
Terraform sends a token to a registry that is named after the registry’s hostname, so a repository that needs one gets it through the Virtual Registry the same way:
export TF_TOKEN_terraform_example_com="<Artifactory access token>"
The variable name is the hostname with each . replaced by _. A credentials "terraform.example.com" block in the CLI configuration does the same.
A request that carries a credential is cached only with the Authentication feature licensed, and then according to the registry’s auth_policy. Without it, such requests go to Artifactory every time. A remote_auth credential on the registry lets clients send none.
JFrog’s cloud service answers a request for a cached provider file with a redirect to its storage, a host such as jfrog-prod-euc1-shared-frankfurt-main.s3.amazonaws.com. With deny_unknown_hosts set, add that host to extra_known_hosts. varnishlog names a refused host in a line such as VS: redirect target host not allowed: <host>.
The registry recognises Terraform by the User-Agent that terraform sends, and does not recognise OpenTofu’s tofu yet. tofu still installs with a source that names the registry, but nothing is cached and no link is rewritten: it fetches provider files from their own hosts, and through an Artifactory remote it goes on to Artifactory itself after service discovery.