Problem
When running terraform init or terraform apply, you may encounter an error indicating a failure to query available provider packages, similar to the following output.
Error: Failed to query available provider packages Could not retrieve the list of available versions for provider hashicorp/helm: could not connect to registry.terraform.io: Failed to request discovery document: Get "https://registry.terraform.io/.well-known/terraform.json": EOF Error: Failed to query available provider packages Could not retrieve the list of available versions for provider hashicorp/google: could not connect to registry.terraform.io: Failed to request discovery document: Get "https://registry.terraform.io/.well-known/terraform.json": EOF
Prerequisites
- You are using a Terraform version between
0.13.0and1.0.0. - You are running Terraform on a macOS, Linux, or other Unix-based platform.
Note: If you are using Terraform 1.0.1 or newer and encounter this error, the cause is likely different. In that case, try refreshing your local DNS cache or configuring your system to use a public DNS server, such as 8.8.8.8.
Cause
There are three primary causes for this error:
- Improper Version Upgrades: Skipping major Terraform versions without following the official Terraform upgrade guides.
-
Missing Provider Source: Not specifying provider sources in your
required_providersblock, which is mandatory for Terraform0.13.0and newer. This also includes typographical errors in provider declarations. - Go DNS Resolution Issue: A known issue with the cross-platform DNS client in the Go programming language, which Terraform is built with. This issue affects DNS lookups on Unix-based platforms, especially when using a VPN.
The Go DNS client defaults to using /etc/resolv.conf and may not correctly follow the operating system's complete native resolver chain. This can lead to DNS lookup failures for provider registries, while other tools like curl succeed because they use the native OS resolver.
If you suspect an issue with the Terraform Registry, you can check the HashiCorp Status page for any reported outages.
This upstream DNS issue was tracked in the Go project and was addressed in Terraform v1.0.1 by building Terraform with native OS resolvers. You can find more details in the Terraform v1.0.1 changelog.
Solutions
Solution 1: Upgrade Terraform
The recommended solution is to upgrade to Terraform v1.0.1 or a newer version. This version includes a fix that uses the native operating system's DNS resolver, which permanently resolves this issue.
Follow the official guide to install or update the Terraform CLI.
Solution 2: Use a Public DNS Server
If you cannot upgrade Terraform immediately, you can configure your system to use a reliable public DNS server. This often resolves the issue by bypassing problematic local DNS resolvers.
For example, you can configure your system's DNS settings to use Google's public DNS server at 8.8.8.8. Refer to your operating system's documentation for instructions on changing DNS servers. See this community-provided example for more context.
Solution 3: Use the OS Native DNS Resolver
As a temporary workaround, you can ensure that Terraform uses your operating system's native DNS resolver by removing any custom DNS server configurations that might interfere. This approach is detailed in this community discussion.
Additional Information
- For further questions, you can engage with the Terraform community on our Discuss forums.
- You can find more community discussions on this topic in these GitHub issues: