'I am trying to run terraform init but getting this error: Failed to query available provider packages

Terraform init is giving the following error. No version has been upgraded and it was working few days back but suddenly it is failing.

Error: Failed to query available provider packages

Could not retrieve the list of available versions for provider hashicorp/aws:
could not connect to registry.terraform.io: Failed to request discovery
document: Get "https://registry.terraform.io/.well-known/terraform.json": read: connection reset by peer


when I run curl from the server, it is not able to connect as well. 
 curl https://registry.terraform.io/
 curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to registry.terraform.io:443


Solution 1:[1]

Are you on a network where an admin might have installed a proxy between you and the internet? If so, you need to get the signing certificates and configure them in your provider.

If you're on a home network or a public one, this is a man in the middle attack. Do not use this network.

If you have the certificates, they can be configured in your aws provider by pointing cacert_path, cert_path and key_path at the appropriate .pem files.

If you have verified that there is a valid reason to have a proxy between you and the internet, you are not touching production, and the certificates are hard to come by, you can test your code by setting insecure = true on your provider. Obviously, don't check that in.

Solution 2:[2]

I get this error from time to time. It's been frequently reported on the terrafrom github page. One particular comment always reminds me to refresh my network settings (e.g. restart network connection):

OK, I think I have isolated and resolved the issue in my case. It's always DNS to blame in the end, right? I hardcoded CloudFlare DNS (1.1.1.1 and its IPv4 and IPv6 aliases) into my network settings on the laptop, and since then everything seems to be working like a treat.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dan Monego
Solution 2 Daniel Viglione