The "Invalid data source" error occurs when Terraform cannot find or recognize a data source in your configuration. This typically happens due to typos in the data source type, missing provider initialization, or using a data source that is not supported by your provider version.
The "Invalid data source" error means Terraform encountered a data source block with a type that does not exist or is not available. A data source in Terraform is a read-only resource used to fetch information about existing infrastructure. When you declare a data source like `data "aws_ami" "example"`, Terraform needs to find the "aws_ami" data source definition in the loaded provider. If the data source type is not recognized, it could be due to several reasons: the provider hasn't been initialized, the provider version doesn't support that data source, or there's a typo in the data source name.
Check the Terraform provider documentation for the exact data source name. Common mistakes include:
# Wrong - typo in data source name
data "aws_ec2_instance" "example" {
filter {
name = "tag:Name"
values = ["my-instance"]
}
}
# Correct - correct data source name
data "aws_instance" "example" {
filter {
name = "tag:Name"
values = ["my-instance"]
}
}Visit the [Terraform AWS Provider documentation](https://registry.terraform.io/providers/hashicorp/aws/latest/docs) and search for your data source to confirm the exact name.
If you haven't already, run terraform init to download and initialize the required providers:
terraform initThis command downloads the provider plugins specified in your required_providers block and creates the necessary working directory structure. Without initialization, Terraform cannot find any data sources.
Ensure the provider is declared in your Terraform configuration with the correct source:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
}If using a third-party provider, use the full source path (e.g., examplecorp/examplecloud).
Some data sources are only available in certain provider versions. Update your provider to the latest compatible version:
terraform init -upgradeThen check the provider changelog or documentation to confirm the data source is available in your version. If not, update the version constraint:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0" # Update to a newer version if needed
}
}
}Use terraform validate to check for syntax errors in your configuration:
terraform validateThis command validates the configuration without accessing any remote services and will report all syntax and reference errors, including invalid data sources.
Ensure all required arguments for the data source are provided. Some data sources have specific requirements:
# Example: aws_ami requires specific filters or owner/name pattern
data "aws_ami" "example" {
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-hvm-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["137112412989"] # Amazon's account ID
}Check the provider documentation for all required and optional arguments.
Provider-specific data sources: Some data sources are only available in specific providers. For example, "aws_ami" only exists in the AWS provider, not in Google Cloud or Azure providers. If migrating between cloud providers, you'll need to rewrite your data source blocks.
Module scope: Data sources within modules follow the same naming rules as root modules. If a data source is defined in a child module, it's scoped to that module and cannot be referenced from other modules directly.
Data source vs Resource: Don't confuse data sources with resources. Resources (declared with resource) create or manage infrastructure, while data sources (declared with data) only read existing infrastructure. Common mistake: trying to use a resource type with the data block syntax.
Caching and state: Data sources are always re-evaluated during terraform plan and terraform apply. They are not stored in the state file like resources, though their results may be referenced by resources.
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