How to fix context deadline exceeded in Terraform
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.
Quick fix
Fixes context deadline exceeded
terraform version- Check Terraform and provider versions
- Add custom timeout configuration to the resource
- Enable debug logging to investigate the timeout
- Check your network connectivity
terraform versioncontext deadline exceeded
What this error means
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.
Typical symptoms
- Terraform apply or plan fails partway through with 'context deadline exceeded'
- Resource creation starts but the timeout interrupts the process before completion
- Error occurs intermittently, sometimes succeeding and sometimes failing
- Applies fail for specific resource types like AWS EC2 instances, RDS databases, or Kubernetes resources that take longer to provision
- State file may be left in an inconsistent state (resource created in cloud but Terraform unaware)
Common causes
- Provider default timeout is too short for the resource being created (most common cause)
- API is throttled (429 status codes) causing delays that exceed the timeout
- Network latency between your machine/CI system and the cloud provider API is high
- Cloud provider is experiencing slow API response times or service degradation
- Large complex resource deployments with multiple dependent operations exceed the cumulative timeout
- Mismatched StateChangeConf timeout and provider-level CRUD function timeout in provider code
How to fix "context deadline exceeded"
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.
Advanced notes
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.
Frequently asked questions
- What does "context deadline exceeded" mean?
- 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.
- How do I fix "context deadline exceeded"?
- 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.
- What causes "context deadline exceeded"?
- Provider default timeout is too short for the resource being created (most common cause) API is throttled (429 status codes) causing delays that exceed the timeout Network latency between your machine/CI system and the cloud provider API is high
- Is the "context deadline exceeded" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.