[Solved] Terraform Error accessing remote module registry in PowerShell

If you are using PowerShell and trying to run a terraform init, you may get an error as follows:

Error: error accessing remote module registry

Failed to retrieve available versions for module ... from registry.terraform.io: Failed to request discovery document: Get "https://registry.terraform.io/.well-known/terraform.json": read tcp x.x.x.x:xxxx->x.x.x.x: wsarecv: An existing connection was forcibly closed by the remote host..

Note that you will still be able to make a successful curl request, but terraform will fail!

The problem

You need to make sure you have a proxy set.

The Windows Command Prompt lets you set a proxy as follows:

SET HTTP_PROXY=http://<USER>:<PASSWORD>@proxy.host.com:8080 SET HTTPS_PROXY=http://<USER>:<PASSWORD>@proxy.host.com:8080 SET NO_PROXY=.yourcompany.com
Code language: Bash (bash)

The solution

PowerShell will still fail, as it required the proxy environment variables to also be set as follows:

[Environment]::SetEnvironmentVariable("HTTP_PROXY","http://<USER>:<PASSWORD>@proxy.host.com:8080/", [EnvironmentVariableTarget]::Process) [Environment]::SetEnvironmentVariable("HTTPS_PROXY","http://<USER>:<PASSWORD>@proxy.host.com:8080/", [EnvironmentVariableTarget]::Process) [Environment]::SetEnvironmentVariable("NO_PROXY",".yourcompany.com", [EnvironmentVariableTarget]::Process)
Code language: plaintext (plaintext)

Notify of
Inline Feedbacks
View all comments