The "error running apply" message indicates Terraform encountered an issue during the apply phase. Common causes include state lock conflicts, provider authentication failures, or resource conflicts. Diagnose by running terraform plan first and checking state file status.
The "error running apply" is a generic error that occurs when the `terraform apply` command fails to complete successfully. During the apply phase, Terraform attempts to create, modify, or destroy infrastructure resources based on your configuration and the execution plan. This error message lacks specificity, making it crucial to investigate the underlying cause by examining logs, state files, and infrastructure provider responses. The error can originate from various sources: Terraform core logic, provider plugins, state file issues, or environmental problems like credentials or network connectivity.
First, run terraform plan to generate an execution plan without modifying infrastructure. This helps identify configuration errors before apply:
terraform plan -out=tfplanReview the output carefully. If terraform plan fails, the issue is likely in your configuration or provider setup. If terraform plan succeeds but apply fails, the problem occurs during resource creation.
A locked state file prevents apply operations. Check if the state is locked:
terraform force-unlock <LOCK_ID>If you see state lock errors, you can attempt to unlock it. First, verify no other Terraform process is running. If using remote state (S3, Terraform Cloud), check the backend configuration and ensure only one process accesses it at a time.
Apply failures often stem from authentication issues. Verify credentials are set correctly for your cloud provider:
For AWS:
export AWS_ACCESS_KEY_ID=your_access_key
export AWS_SECRET_ACCESS_KEY=your_secret_key
export AWS_REGION=us-east-1For Azure:
az loginFor GCP:
gcloud auth application-default loginEnsure credentials have permissions to create/modify resources in your plan.
Enable Terraform debug logging to see detailed error messages:
export TF_LOG=DEBUG
terraform apply -auto-approveThis generates verbose output showing exactly where the apply failed. Review the logs for provider-specific error messages that reveal the root cause (quota limits, permission errors, API failures, etc.).
Ensure your Terraform configuration is syntactically correct and all references are valid:
terraform validate
terraform fmt -checkThe validate command checks for syntax errors and missing required arguments. The fmt command identifies formatting issues. Fix any reported errors before attempting apply again.
Ensure your provider version is compatible with your configuration. Lock provider versions in production:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}Run terraform init after updating provider requirements to download the correct version.
Check if the state file is corrupted or out of sync:
terraform state list
terraform state show <resource_name>If state shows resources that no longer exist in infrastructure, use terraform refresh or terraform import to sync state with actual infrastructure. Out-of-sync state often causes apply failures.
State management is critical when apply fails. In team environments, always use remote state (Terraform Cloud, S3, etc.) to prevent concurrent modifications. If you're using Terraform Cloud, check organization policies and cost estimation settings—some organizations require approval before apply. For large infrastructure changes, consider using terraform apply -parallelism=1 to apply resources sequentially, which helps identify which resource actually failed. When dealing with provider errors, check the provider's GitHub repository for known issues in your version. Some providers have regional limitations or require feature flags enabled in the cloud provider console before Terraform can use them.
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