How to fix Error: Invalid data source in Terraform
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.
Quick fix
Fixes Error: Invalid data source
# 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"]
}
}- Verify the data source type name
- Run terraform init to initialize providers
- Declare the provider in required_providers
- Check provider version compatibility
# Wrong - typo in data source namedata "aws_ec2_instance" "example" {filter {name = "tag:Name"values = ["my-instance"]}}# Correct - correct data source namedata "aws_instance" "example" {filter {name = "tag:Name"values = ["my-instance"]}}Error: Invalid data source
What this error means
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.
Typical symptoms
- Error message: "Error: Invalid data source" followed by the data source type
- Error occurs during terraform plan or terraform apply
- Error message includes the data source name that cannot be found
- Other parts of the configuration may still be valid
- The error appears immediately when Terraform processes the configuration
Common causes
- Typo or spelling error in the data source type name
- Provider not initialized (terraform init not run)
- Provider version does not support the data source
- Data source does not exist in that specific provider
- Using a data source from a provider without declaring it in required_providers
- Using a third-party data source without the correct provider source URL
How to fix "Error: Invalid data source"
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.
Advanced notes
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.
Frequently asked questions
- What does "Error: Invalid data source" mean?
- 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.
- How do I fix "Error: Invalid data source"?
- 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.
- What causes "Error: Invalid data source"?
- Typo or spelling error in the data source type name Provider not initialized (terraform init not run) Provider version does not support the data source
- Is the "Error: Invalid data source" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.