Terraform - Conditional Data Source
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Terraform does not have an if statement that wraps an entire data block directly, but you can still make data-source lookup conditional. The usual pattern is to use count or for_each so the data source is instantiated only when the condition is true. After that, you reference it carefully because the data source may not exist in the false branch.
Use count for a simple boolean condition
A common pattern looks like this:
If lookup_ami is false, the data source count is zero and Terraform does not instantiate it.
Reference conditional data sources safely
Once count is involved, the data source becomes a list-like collection. So references must use an index:
That conditional matters. If you try to access data.aws_ami.selected[0].id when count = 0, Terraform will fail.
A related defensive tool is try():
This is useful when the data source may or may not exist.
for_each is useful when the condition is collection-shaped
If the lookup should happen for each enabled item in a set or map, for_each is often cleaner than count:
This pattern is handy when the condition is naturally keyed rather than just true or false.
Ask whether the lookup really belongs in the false branch
A good Terraform design question is whether the data source should be skipped entirely or whether the module should always receive the looked-up value and decide later how to use it.
Conditional data sources are useful, but they also add reference complexity. Sometimes a simpler design is:
- always read the data
- conditionally use the result later
That depends on API cost, provider behavior, and whether the lookup is valid in all environments.
Structure outputs with the conditional in mind
If the rest of the module consumes the looked-up value, think about what the "disabled" shape should be. Returning null, an empty list, or an empty map can make downstream expressions much easier to reason about. The conditional data-source pattern works best when the absence case is modeled explicitly instead of being left for later resources to discover accidentally.
Common Pitfalls
The most common mistake is writing a conditional count on the data source and then referencing it as though it were always a single object rather than a counted collection.
Another common issue is forgetting to guard the [0] access when count may be zero.
People also overuse conditional data sources when always performing the lookup would have made the module simpler and easier to read.
Finally, remember that the condition affects whether Terraform evaluates that block at all. If your provider or input assumptions differ across environments, that can be exactly what you want, but it should be intentional.
Summary
- Terraform does conditional data-source lookup through
countorfor_each, not a dedicatedifblock. - Use
count = condition ? 1 : 0for simple boolean control. - Reference counted data sources carefully because they become indexed collections.
- '
try()and guarded conditionals help avoid invalid references when the data source does not exist.' - Sometimes the simpler design is to always read the data and conditionally use it later.
Related reading
- Terraform and Helm3 Error Kubernetes cluster unreachable
- Terraform AWS credentials file not found
- Terraform AWS Kubernetes EKS resources with ALB Ingress Controller won't create load balancer
- Terraform AWS Provider Error Value for unconfigurable attribute. Can't configure a value for acl its value will be decided automatically
- Terraform configuring cloudwatch log subscription delivery to lambda?
- Terraform cycle with AWS and Kubernetes provider
- Terraform error - RDS Cluster FinalSnapshotIdentifier is required when a final snapshot is required
- Terraform Error creating IAM Role. MalformedPolicyDocument Has prohibited field Resource

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.