Problem
When running terraform plan or init, you may encounter an error indicating a DNS resolution failure. The error message resembles the following output:
Error: Failed to request discovery document: Get "https://app.terraform.io/.well-known/terraform.json": dial tcp: lookup app.terraform.io on 127.0.0.53:53: server misbehaving
Note: The hostname, such as app.terraform.io, may differ depending on your HCP Terraform or Terraform Enterprise configuration.
Prerequisites
- This issue is known to affect Ubuntu 18.04 environments.
Cause
This error is caused by a known issue with systemd-resolved in Ubuntu 18.04, which can cause DNS resolution to fail intermittently. You can find more details in the official bug report: Bug #1988119 on Launchpad.
Solutions
Choose one of the following solutions to resolve the DNS issue.
Solution 1: Reboot the instance
A system reboot will restart all services, including the networking components, which typically resolves the issue.
Solution 2: Restart network services
If you cannot reboot the instance, you can resolve the issue by restarting the network services. Run the following command as the root user:
# udevadm trigger -cadd -yeth0 && systemctl restart systemd-networkd
Outcome
After applying one of the solutions, the DNS resolution issue should be resolved, allowing Terraform commands to connect to the remote service successfully.