How to fix Error: Error making HTTP request: connection refused in Terraform

TerraformINTERMEDIATEHIGHUpdated Dec 31, 2025· 153 views

This error occurs when Terraform cannot establish an HTTP connection, typically because the target server is unreachable or not listening. Common causes include uninitialized infrastructure, incorrect endpoints, or network connectivity issues.

Quick fix

Fixes Error: Error making HTTP request: connection refused

# Test connection to the endpoint
curl -v http://your-endpoint:port

# For Kubernetes, verify cluster is running
kubectl cluster-info
  1. Verify the target endpoint is accessible and running
  2. Check network connectivity and firewall rules
  3. Separate infrastructure creation into multiple states (recommended)
  4. Use resource references instead of data sources in provider config
terraform — error
# Test connection to the endpoint
curl -v http://your-endpoint:port
# For Kubernetes, verify cluster is running
kubectl cluster-info
Error: Error making HTTP request: connection refused

What this error means

This error happens when Terraform tries to make an HTTP request (through data sources or providers) but cannot establish a connection to the specified endpoint. The "connection refused" message means either the server is not listening on that port, the host is unreachable, or firewall/network rules are blocking the connection. This frequently occurs with Kubernetes providers that attempt to reach a cluster endpoint before the cluster exists, or when provider configuration uses data sources that reference resources not yet created. Terraform cannot resolve these dependencies at plan time, causing it to default to localhost.

Typical symptoms

  • terraform plan or terraform apply fails with 'connection refused' error
  • Error message shows 'dial tcp 127.0.0.1:80' or similar localhost address
  • The error occurs immediately when applying infrastructure that creates both cluster and resources
  • Error appears during destroy when dependent resources try to access removed infrastructure
  • The same configuration works fine on subsequent applies

Common causes

  1. Provider configuration uses data sources that depend on resources being created in the same apply (circular dependency)
  2. Target server or endpoint does not exist or is not running yet
  3. Network connectivity is blocked by firewall rules or security groups
  4. Incorrect endpoint URL or IP address in provider configuration
  5. Service is still starting up and not yet accepting connections

How to fix "Error: Error making HTTP request: connection refused"

1Verify the target endpoint is accessible and running

Check that the server you're trying to connect to is actually running and listening on the specified port. For Kubernetes clusters, verify the cluster exists and the endpoint is correct.

For local services or clusters:

bash
# Test connection to the endpoint
curl -v http://your-endpoint:port

# For Kubernetes, verify cluster is running
kubectl cluster-info

If the endpoint is unreachable or the service isn't running, start the service first before running Terraform.

2Check network connectivity and firewall rules

Ensure network connectivity exists between your Terraform execution environment and the target server. Verify that firewall rules, security groups, and network ACLs allow traffic on the required port.

bash
# Test if port is open
nc -zv endpoint-host port

# For AWS security groups, verify egress rules
# For Azure NSGs, check outbound rules

Configure firewall and network rules to allow the connection. If running behind a proxy, configure it in the provider.

3Separate infrastructure creation into multiple states (recommended)

The most reliable fix for Kubernetes/cluster errors: deploy cluster creation separately from resource creation.

Instead of single apply:

hcl
# ❌ Do NOT do this - cluster and kubernetes resources in one apply
provider "kubernetes" {
  host = data.aws_eks_cluster.cluster.endpoint  # Data source depends on aws_eks_cluster!
  token = data.aws_eks_cluster_auth.cluster.token
}

resource "aws_eks_cluster" "cluster" { ... }
resource "kubernetes_namespace" "example" { ... }

Use separate state files:

hcl
# ✅ First state: Create cluster
# terraform/cluster/main.tf
resource "aws_eks_cluster" "cluster" { ... }

# ✅ Second state: Create Kubernetes resources
# terraform/kubernetes/main.tf
provider "kubernetes" {
  host = "https://actual-cluster-endpoint.eks.amazonaws.com"  # Direct reference
  token = data.aws_eks_cluster_auth.cluster.token
}

Deploy in two separate runs:

bash
cd terraform/cluster && terraform apply
cd ../kubernetes && terraform apply

This ensures the cluster exists before Kubernetes provider attempts connection.

4Use resource references instead of data sources in provider config

When creating both infrastructure and resources in the same apply, reference the resource directly instead of using data sources:

hcl
# ❌ Problematic (data source may read before resource exists)
provider "kubernetes" {
  host = data.aws_eks_cluster.default.endpoint
  token = data.aws_eks_cluster_auth.default.token
}

# ✅ Better (direct resource reference)
provider "kubernetes" {
  host = aws_eks_cluster.cluster.endpoint
  token = data.aws_eks_cluster_auth.cluster.token
}

resource "aws_eks_cluster" "cluster" {
  name = "my-cluster"
  ...
}

This gives Terraform explicit dependency information.

5Adjust parallelism and retry if needed

Some connection refused errors are intermittent timing issues. Try reducing parallelism:

bash
# Reduce parallel operations
terraform apply -parallelism=1

Also verify no environment variables override your configuration:

bash
# Check for conflicting KUBE* or provider-specific env vars
env | grep -i kube
env | grep -i aws
env | grep -i azure

Remove any conflicting environment variables and retry.

6Configure request timeouts and retries (for HTTP data source)

For HTTP data source specifically, configure timeouts and retries:

hcl
data "http" "example" {
  url = "http://example.com/api"

  # Add timeout
  request_timeout_ms = 10000  # 10 seconds

  # Retry on transient failures (go-retryablehttp)
  retry_policy = {
    retry_on_error = true
    retry_max = 3
    retry_interval_ms = 1000
  }
}

This helps with temporary connectivity issues or slow-starting services.

Advanced notes

This error is especially common in Kubernetes deployments because of how Terraform provider initialization works. Providers are initialized during planning phase, before resources are created. If your provider config depends on values from resources being created (like cluster endpoints), Terraform cannot satisfy this dependency and falls back to defaults (usually localhost).

The HashiCorp team strongly recommends separating infrastructure into multiple state files for production Kubernetes deployments. For simpler cases with good timing, using resource references instead of data sources can work, but it's less reliable.

For EKS specifically, use managed node groups and ensure security group egress rules allow cluster API access. For GKE, verify that client_certificate_config is properly configured. For AKS, ensure service principal has cluster access.

Frequently asked questions

What does "Error: Error making HTTP request: connection refused" mean?
This error happens when Terraform tries to make an HTTP request (through data sources or providers) but cannot establish a connection to the specified endpoint. The "connection refused" message means either the server is not listening on that port, the host is unreachable, or firewall/network rules are blocking the connection. This frequently occurs with Kubernetes providers that attempt to reach a cluster endpoint before the cluster exists, or when provider configuration uses data sources that reference resources not yet created.
How do I fix "Error: Error making HTTP request: connection refused"?
This error occurs when Terraform cannot establish an HTTP connection, typically because the target server is unreachable or not listening. Common causes include uninitialized infrastructure, incorrect endpoints, or network connectivity issues.
What causes "Error: Error making HTTP request: connection refused"?
Provider configuration uses data sources that depend on resources being created in the same apply (circular dependency) Target server or endpoint does not exist or is not running yet Network connectivity is blocked by firewall rules or security groups
Is the "Error: Error making HTTP request: connection refused" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading