TerraformPilot

DevOps

Fix Terraform Error: Context Deadline Exceeded (API Timeout)

Fix terraform context deadline exceeded errors caused by API timeouts. Covers retry, reduced parallelism, custom timeouts, network debugging

LLuca Berton1 min read

Quick Answer

#
# Usually succeeds on retry
terraform apply
 
# If it keeps failing, reduce parallelism
terraform apply -parallelism=5

The Error

#
Error: creating EC2 Instance: Post "https://ec2.us-east-1.amazonaws.com/": 
context deadline exceeded
 
Error: waiting for RDS Instance (mydb) create: context deadline exceeded

Or:

Error: Provider "aws" produced an unexpected error: 
Post https://sts.amazonaws.com/: context deadline exceeded (Client.Timeout exceeded)

What Causes This

#
  1. API throttling — too many concurrent requests hit rate limits
  2. Slow resource creation — RDS, EKS, CloudFront can take 10-30 minutes
  3. Network issues — VPN, proxy, DNS resolution failures
  4. Large state — hundreds of resources cause slow plan/refresh

Solution 1: Retry

#

Most transient. Just run it again:

terraform apply

Terraform tracks what was created — it picks up where it left off.

Solution 2: Reduce Parallelism

#

Default parallelism is 10. Lower it when hitting API rate limits:

# Reduce to 5 concurrent operations
terraform apply -parallelism=5
 
# Very aggressive — useful for debugging
terraform apply -parallelism=1

Solution 3: Custom Resource Timeouts

#

Some resources take a long time. Override the default timeout:

resource "aws_db_instance" "main" {
  engine         = "postgres"
  instance_class = "db.r6g.xlarge"
  # ...
 
  timeouts {
    create = "60m"   # Default is 40m
    update = "80m"
    delete = "60m"
  }
}
 
resource "aws_eks_cluster" "main" {
  name     = "my-cluster"
  role_arn = aws_iam_role.eks.arn
  # ...
 
  timeouts {
    create = "30m"
    update = "60m"
    delete = "15m"
  }
}

Common Resources That Need Extended Timeouts

#
ResourceDefaultRecommended
aws_db_instance40m60m
aws_eks_cluster30m45m
aws_cloudfront_distribution70m90m
aws_rds_cluster120m120m
aws_elasticsearch_domain60m90m
azurerm_kubernetes_cluster90m120m

Solution 4: Debug Network Issues

#
# Test connectivity to AWS API
curl -v https://ec2.us-east-1.amazonaws.com/
 
# Check DNS resolution
nslookup ec2.us-east-1.amazonaws.com
 
# Enable Terraform debug logging
export TF_LOG=DEBUG
terraform apply 2>&1 | tee terraform-debug.log
 
# Check for proxy issues
echo $HTTPS_PROXY $HTTP_PROXY $NO_PROXY

Solution 5: Target Large Deployments

#

Break up massive applies:

# Deploy in stages
terraform apply -target=module.networking
terraform apply -target=module.database
terraform apply -target=module.application
terraform apply  # Final pass for remaining resources

Solution 6: Increase HTTP Timeout (Provider Level)

#
provider "aws" {
  region = "us-east-1"
 
  # Increase HTTP client timeout
  max_retries = 10
 
  default_tags {
    tags = {
      ManagedBy = "terraform"
    }
  }
}

Prevention

#
  • Use -parallelism=5 in CI/CD pipelines by default
  • Add timeouts blocks to slow resources (RDS, EKS, CloudFront)
  • Split large root modules into smaller ones
  • Use terraform plan -refresh=false for faster planning when debugging

Hands-On Courses

#

Conclusion

#

Context deadline exceeded means an API call timed out. Retry first — it usually works. If it keeps happening, reduce parallelism to 5, add timeouts blocks to slow resources, and check for network/VPN issues. For CI/CD, always set -parallelism=5 and add generous timeouts to RDS, EKS, and CloudFront resources.

#Terraform#Troubleshooting#DevOps#Error Fix#Infrastructure as Code

Share this article