If you're running Terraform on an Apple M1 or M2 chip and encountering initialization failures, provider cache issues, or mysterious panics, you're not alone. These issues are common when Terraform fails to properly detect or upgrade cached providers, especially when you've changed versions but the cache still contains old or corrupted provider binaries.
This guide will walk you through the most effective solutions to get Terraform working smoothly on Apple Silicon.
Common Issues on Apple M1/M2
The most frequent problems include:
- Provider initialization failures - Terraform can't properly initialize or upgrade providers
- Corrupted cached providers - Old or incompatible provider binaries in cache
- Version conflicts - Terraform finds cached versions instead of using the specified version
- Async preemption panics - Go runtime issues specific to Apple Silicon architecture
Solution 1: Clean Terraform State and Reinitialize
The most straightforward fix is to clean your Terraform working directory and force a fresh initialization:
# Remove the .terraform directory and lock file
rm -rf .terraform
rm -f .terraform.lock.hcl
# Reinitialize with upgrade flag
terraform init -upgrade
This forces Terraform to:
- Download fresh provider binaries
- Recreate the dependency lock file
- Ensure compatibility with your current Terraform version
Solution 2: Set Go Debug Environment Variable
Apple Silicon chips sometimes have issues with Go's async preemption feature. Set this environment variable before running Terraform:
export GODEBUG=asyncpreemptoff=1
terraform init -upgrade
To make this permanent, add it to your shell profile:
# Add to ~/.zshrc or ~/.bash_profile
echo 'export GODEBUG=asyncpreemptoff=1' >> ~/.zshrc
source ~/.zshrc
Solution 3: Clear Plugin Cache Directory
If you're using a plugin cache directory (via .terraformrc or TF_PLUGIN_CACHE_DIR), clear it:
# Check if you have a .terraformrc file
cat ~/.terraformrc
# If it shows a plugin_cache_dir, clear it
rm -rf ~/.terraform.d/plugin-cache/*
Or if you have TF_PLUGIN_CACHE_DIR set in your environment:
# Check your environment
echo $TF_PLUGIN_CACHE_DIR
# Clear the cache directory
rm -rf $TF_PLUGIN_CACHE_DIR/*
Solution 4: Complete Reset (Nuclear Option)
If the above solutions don't work, perform a complete reset:
# 1. Clean your project directory
rm -rf .terraform
rm -f .terraform.lock.hcl
# 2. Clear global Terraform cache
rm -rf ~/.terraform.d/plugin-cache/*
# 3. Set the Go debug flag
export GODEBUG=asyncpreemptoff=1
# 4. Reinitialize
terraform init -upgrade
Solution 5: Provider-Specific Fixes
For specific providers that might have Apple Silicon compatibility issues:
AWS Provider
# Force download of latest AWS provider
terraform init -upgrade -backend=false
Azure Provider
# Clear Azure-specific cache
rm -rf ~/.azure/terraform
terraform init -upgrade
Prevention Tips
To avoid these issues in the future:
- Always use version constraints in your
versions.tf:terraform { required_version = ">= 1.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } }
}
2. **Commit your `.terraform.lock.hcl`** file to version control to ensure consistent provider versions across environments.
3. **Use consistent Terraform versions** across your team and CI/CD pipelines.
4. **Regularly update providers** with `terraform init -upgrade` when appropriate.
## Troubleshooting Commands
Use these commands to diagnose issues:
```bash
# Check Terraform version
terraform version
# Check provider versions
terraform providers
# Validate configuration
terraform validate
# Check for any remaining issues
terraform plan
When to Use Each Solution
- Solution 1: Use when you're getting initialization errors or version conflicts
- Solution 2: Use when you're getting Go runtime panics or async preemption errors
- Solution 3: Use when you have a plugin cache directory configured
- Solution 4: Use when nothing else works or you want a completely fresh start

