From Zero to Hero: Build your first voice agent with Voice Live API
May 27, 2025The Career Story of a Nigerian MVP Who Paved the Way to Becoming a Technologist
May 27, 2025Terraform is an Infrastructure as Code (IaC) tool that allows you to define, manage, and provision cloud resources efficiently. While Terraform is often used to create and modify resources, sometimes you need to retrieve information about existing infrastructure, whether it’s managed by Terraform or provisioned externally. This is where data sources come in.
In this blog, we’ll explore what data sources are, how they differ from Terraform variables and resources, and how to use them effectively in your configurations.
What is a data source in Terraform?
Data sources in Terraform allow you to dynamically retrieve information about existing infrastructure, whether it is managed within Terraform or provisioned externally.
Data sources enable Terraform configurations to reference real-time data about cloud resources, services, or other dependencies without directly creating or modifying them. This is particularly useful when integrating Terraform with pre-existing environments, reusing shared infrastructure components, or querying dynamic attributes that may change over time.
By leveraging data sources, you can ensure your configurations remain flexible, adaptable, and aligned with the current state of your infrastructure.
What is the difference between Terrafrom variables and data sources in Terraform?
Terraform variables and data sources serve different purposes within the infrastructure provisioning workflow.
Variables are used to parameterise your Terraform configurations, they allow you to define reusable values. This allows you to configure different environments easily by having a centralised place where you can change values easily.
Data sources are used to retrieve information that you can incorporate into your configuration. Data sources query for information they don’t change or move resources under Terraform management.
Are Terraform resources and data sources in Terraform the same?
Within Terraform, resources represent the infrastructure components that we want to create, update or delete. A resource could be a virtual machine, a database, a storage account or more.
Data sources are used to retrieve information that you can incorporate into your configuration. Data sources query for information they don’t change or move resources under Terraform management.
Terraform data sources vs locals
Local variables within Terraform are used to store and reuse values within your Terraform configuration. They allow you to keep your configuration DRY (Don’t Repeat Yourself) by defining a value in one and then referencing it in multiple locations.
Data sources are used to retrieve information that you can incorporate into your configuration. Data sources query for information they don’t change or move resources under Terraform management.
– What are Terraform modules
– Introduction to Terraform outputs
How to use data sources in Terraform
To use a data source and query information, use the following syntax:
data “provider_type” {
#Configuration options
}
In that syntax, the “provider_type! Would be the type of object we want to query. And the name is something we define so we can refer to them elsewhere in the configuration. Within the resource block section, we can define options that help us filter conditions so we can fetch the data we need.
An example would be:
data "azurerm_log_analytics_workspace" "observability_log_analytics_workspace" {
name = “laworkspace”
resource_group_name = “rgobservability”
}
Here, I am fetching data about an Azure Log Analytics Workspace. I’ve told the Terraform configuration its name and the resource group it currently resides in.
Once the data source is defined, you can use it within resources like any other variable. For instance, the following resource configuration enables diagnostic logging for an Azure Communication Service and sends logs to the queried Log Analytics Workspace:
# Turn on logs being sent to a Log Analytics Workspace
resource "azurerm_monitor_diagnostic_setting" "acs-logs" {
name = var.diagnostic_name
target_resource_id = data.azurerm_communication_service.acs
log_analytics_workspace_id = data.azurerm_log_analytics_workspace.observability_log_analytics_workspace.id
log_analytics_destination_type = "Dedicated"
enabled_log {
category = "EmailSendMailOperational"
}
metric {
category = "AllMetrics"
}
}
Refreshing data sources
Terraform will refresh data sources before creating a plan. So you don’t necessarily need to refresh them manually; however, if you would like to, then you can use the -refresh-only flag along with Terraform plan or Terraform apply.
Conclusion
Terraform data sources are a great feature that allows you to dynamically retrieve information about existing infrastructure without making changes to it.
By leveraging data sources, you can create more flexible, reusable, and scalable configurations that adapt to real-world environments.
Whether you are integrating with pre-existing cloud resources, avoiding hardcoded values, or ensuring consistency across multiple deployments, data sources help you build smarter and more efficient Terraform code.
Now that you have a solid understanding of how data sources work, try incorporating them into your Terraform projects to streamline your infrastructure management.