Table of Contents

The Error

Error: No valid credential sources found

What Causes This

Terraform cannot find valid credentials to authenticate with your cloud provider. This happens when environment variables are missing, credential files are misconfigured, IAM roles aren’t attached, or SSO sessions have expired.

How to Fix It

AWS Solutions

# Option 1: Environment variables
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_REGION="us-east-1"

# Option 2: AWS CLI profile
aws configure
# Then reference in Terraform:
provider "aws" {
  region  = "us-east-1"
  profile = "my-profile"
}
# Option 3: SSO — refresh session
aws sso login --profile my-sso-profile

# Option 4: Check IAM role (EC2/ECS)
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/

Azure Solutions

# Login via CLI
az login
az account set --subscription "your-subscription-id"

# Or use service principal
export ARM_CLIENT_ID="xxx"
export ARM_CLIENT_SECRET="xxx"
export ARM_SUBSCRIPTION_ID="xxx"
export ARM_TENANT_ID="xxx"

GCP Solutions

# Application default credentials
gcloud auth application-default login

# Or service account key
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json"

CI/CD Best Practice

# Never hardcode credentials — use environment variables
provider "aws" {
  region = var.aws_region
  # Credentials come from environment
}

Prevention Tips

  1. Pin provider versions — avoid surprise breaking changes
  2. Use CI/CD — catch errors before they hit production
  3. Test with terraform plan — always review before applying
  4. Keep Terraform updated — newer versions have better error messages
  5. Use terraform validate — catches syntax errors early

Hands-On Courses

Learn to avoid these errors with interactive, project-based courses:

Conclusion

This error is common and fixable. Follow the solutions above, and check our Terraform course for hands-on training that covers real-world troubleshooting scenarios.