Context deadline exceeded occurs when a Terraform provider API call times out before receiving a response. This typically indicates the client timeout is shorter than the API operation duration, particularly common with large cloud resource deployments.
This error happens when Terraform's provider SDK reaches a deadline (timeout) before an API operation completes. The underlying Go context has a deadline set, and if the API call exceeds this timeout, the context deadline is exceeded. This is different from a general network timeout—it means the provider actively canceled the operation because it took too long based on configured timeouts.
First, ensure you're running the latest versions of Terraform and your cloud provider. Timeout handling improvements are frequently added in updates.
terraform versionUpgrade if needed:
terraform init -upgradeIncrease the timeout for resources known to take a long time. Add a timeouts block directly to your resource:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
timeouts {
create = "15m"
update = "20m"
delete = "10m"
}
}For Azure resources, use:
resource "azurerm_app_service" "example" {
name = "example-app"
# ... other config ...
timeouts {
create = "1h"
update = "1h"
delete = "30m"
}
}Run Terraform with trace logging to get detailed information about where the timeout occurs:
export TF_LOG=trace
terraform apply 2>&1 | tee terraform-debug.logSearch the log for timing information, API calls being made, and the exact point where the deadline was exceeded. This helps identify if it's a specific API call or a cumulative issue.
Verify you have stable, low-latency connectivity to the cloud provider's API endpoints:
# For AWS
ping dynamodb.us-east-1.amazonaws.com
# For Azure
ping management.azure.com
# For Terraform registry
ping registry.terraform.ioIf you're behind a corporate proxy or VPN, verify the proxy is not adding significant latency or dropping requests.
If deploying many resources at once, reduce parallelism and split into smaller deployments:
# Reduce default parallelism from 10 to 2
terraform apply -parallelism=2Or create multiple Terraform modules/workspaces deployed separately. This reduces the number of concurrent API calls and gives each one more time to complete.
If you see HTTP 429 status codes with the context deadline error, the cloud provider is rate-limiting your requests:
# Look for 429 in the debug log
grep -i '429\|throttl' terraform-debug.logSolutions include:
- Waiting before retrying
- Reducing the number of resources created simultaneously
- Contacting your cloud provider to increase rate limits
- Using Terraform's built-in retry logic by just running apply again
If the resource was actually created in the cloud but Terraform's state wasn't updated due to timeout:
# Find the resource ID in the cloud console
terraform import aws_instance.example i-1234567890abcdef0This brings the already-created resource into Terraform's state management, allowing you to continue with future applies.
Context deadline exceeded can manifest differently across providers. Azure resources (API Management, Application Insights, Private Endpoints) are particularly prone to this error due to longer provisioning times. AWS generally has better timeout handling, but large deployments can still hit issues.
The root cause in Terraform providers is usually a mismatch: the provider-level CRUD function has a context deadline (often 20 minutes default), but internal StateChangeConf retries must complete within that same deadline. If StateChangeConf timeout is configured above the context deadline, the context deadline triggers first, returning an unhelpful error instead of the actual retry error.
Provider developers address this by ensuring StateChangeConf timeout < CRUD context timeout. As a practitioner, increasing the resource-level timeout ensures both have adequate time.
Error: Error installing helm release: cannot re-use a name that is still in use
How to fix "release name in use" error in Terraform with Helm
Error: Error creating GKE Cluster: BadRequest
BadRequest error creating GKE cluster in Terraform
Error: External program failed to produce valid JSON
External program failed to produce valid JSON
Error: Unsupported argument in child module call
How to fix "Unsupported argument in child module call" in Terraform
Error: network is unreachable
How to fix "network is unreachable" in Terraform