Skip to main content
Fix Terraform Error - Data Source Not Found

Fix Terraform Error - Data Source Not Found

Key Takeaway

Learn how to fix data source not found and empty result errors in Terraform for AMIs, VPCs, subnets, and availability zones with practical debugging steps.

Table of Contents

The Error

Error: no matching data source found / Your query returned no results

What Causes This

The data source query returned zero results. The resource doesn’t exist, filters are too restrictive, or you’re in the wrong region.

How to Fix It

Solution 1: Use Wildcards in Filters

data "aws_ami" "ubuntu" {
  most_recent = true
  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-*"]
  }
  owners = ["099720109477"]
}

Solution 2: Verify with CLI

aws ec2 describe-images --owners 099720109477 \
  --filters "Name=name,Values=ubuntu*jammy*" \
  --query 'Images | sort_by(@, &CreationDate) | [-1].ImageId'

Solution 3: Check Region

provider "aws" {
  region = "us-east-1"  # AMIs are region-specific!
}

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

Conclusion

Related: AWS: Increase EC2 root_block_device size — resize your EC2 storage with Terraform.

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.

🚀

Level Up Your Terraform Skills

Hands-on courses, books, and resources from Luca Berton

Luca Berton
Written by

Luca Berton

DevOps Engineer, AWS Partner, Terraform expert, and author. Creator of Ansible Pilot, Terraform Pilot, and CopyPasteLearn.