TerraformHigh
Fix: Terraform Apply Timeout on Resource Creation
terraform apply hangs and eventually times out
!Symptoms
- terraform apply hangs and eventually times out
- Resource created in cloud console but Terraform doesn't know about it
- State file out of sync with actual infrastructure
- Subsequent applies fail with 'already exists' errors
?Root Causes
- Cloud resource taking longer to provision than Terraform's timeout
- API rate limiting causing delayed responses
- Dependency chain creating resources sequentially instead of in parallel
- Network issues between Terraform runner and cloud API endpoints
- Resource stuck in a transitioning state (e.g., RDS modifying, ALB provisioning)
#Diagnosis Steps
- 1Check cloud console for the resource status
- 2Run `terraform plan` to see if state is out of sync
- 3Check Terraform debug logs: `TF_LOG=DEBUG terraform apply`
- 4Verify API connectivity: `curl https://<cloud-api-endpoint>`
- 5Check for API rate limit errors in the logs
>Fix
- 1Import the created resource: `terraform import <resource_type>.<name> <cloud-id>`
- 2Increase create timeout: `timeouts { create = "60m" }` in the resource block
- 3If state corrupted, remove and re-import: `terraform state rm <resource>` then `terraform import`
- 4Add retry logic with `retries` in the provider configuration
- 5Use `-parallelism=5` to reduce concurrent API calls and avoid rate limiting
*Prevention
- Set explicit timeouts on resources known to be slow (RDS, EKS, CloudFront)
- Use Terraform Cloud or CI runners with stable network connectivity
- Implement state locking (S3+DynamoDB, GCS, Azure Blob) to prevent corruption
- Break large infrastructure into smaller Terraform workspaces/modules
- Monitor Terraform apply duration and set pipeline timeouts accordingly
Related Error Messages
timeout while waiting for state to becomeError waiting for creationcontext deadline exceededalready exists