Data Sources
Using data blocks to read information that is not managed by the current Terraform configuration.
Resource blocks vs data blocks
-
Resource blocks creates things. A
resourceblock tells Terraform to manage an object for the whole lifecycle: create it, update it when the code changes, and destroy it when it's removed. -
Data blocks only read things, meaning a
datablock asks a provider for information that already exists, without managing it. I.e. something built by hand in the console, created by another Terraform configuration or simply provided by the platform, such as the latest Ubuntu AMI, list of availability zones or the account you are currently logged in as.
Key difference here is that Terraform never creates, changes or destroys anything through a data source, running terraform destroy leaves the object untouched.
Both come from providers, so a provider can offer resources, data sources, even both. In the documentation, they usually share a name, for example the hcloud_image resource and the hcloud_image data source.
Defining a data source
A data block looks just like a resource block, but starts with data.
data "hcloud_image" "by_name_x86" {
name = "debian-12"
with_architecture = "x86"
}
It has a type and a local name, in this example type it's type is "hcloud_image" and local name is "by_name_x86".
Inside it, you give arguments that will act as query filtes, such as a name, and an architecture - this does not mean that Terraform is building it, you are telling it how to look for the object.
Terraform will read data sources during plan, so the values are known up front. If an argument is dependent on a value that won't be known until apply, the read is pushed back to the apply step.
If the query doesn't find anything, or if there is more than one match, the plan fails with an error - useful as an early check so that something you depend on, actually exists.
Data sources results are stored in state too, but only as a cached copy. Terraform does not own those objects.
Referencing a data source elsewhere
Using the pattern data.<TYPE>.<NAME>.<ATTRIBUTE>, for example data.hcloud_image.by_name_x86.name to pass the OS image name into a Hetzner Cloud server:
data "hcloud_image" "by_name_x86" {
name = "ubuntu-24.04"
with_architecture = "x86"
}
resource "hcloud_server" "web" {
name = "web-1"
server_type = "cx22"
image = data.hcloud_image.by_name_x86.name
}
Data sources need the data. prefix, resources don't:
hcloud_server.web.id # resource
data.hcloud_image.by_name_x86.id # data source
If the data block uses count or for_each, add the index or key:
data.hcloud_network.private[0].id # count
data.hcloud_network.private["a"].id # for_each
To read another configuration's outputs, use the terraform_remote_state data source. This is a very common way of sharing values between separate configurations:
data "terraform_remote_state" "network" {
backend = "local"
config = {
path = "../network/terraform.tfstate"
}
}
resource "hcloud_server_network" "web" {
server_id = hcloud_server.web.id
network_id = data.terraform_remote_state.network.outputs.network_id
}
You can also pass data source attributes into outputs, locals and module inputs, the same way you would resource attribute.