How Terraform manages infrastructure with resource blocks, reads existing information with data sources, and connects values with references.
A resource block declares infrastructure Terraform should manage. A data block reads information exposed by a provider without creating or taking ownership of the associated object. References carry those values through the configuration and usually create dependency edges. See HashiCorp’s resource and data-source references.
| Need | Use |
|---|---|
| Create or manage an object’s lifecycle | resource |
| Look up an existing object or service value | data |
| Use an attribute from another declared object | An expression reference |
1data "aws_vpc" "shared" {
2 tags = { Name = "shared-network" }
3}
4
5resource "aws_subnet" "app" {
6 vpc_id = data.aws_vpc.shared.id
7 cidr_block = "10.0.10.0/24"
8}
The VPC remains outside this configuration’s management; Terraform reads its ID and uses it to create the subnet. If the configuration should manage the VPC too, declare it as a resource instead. A data source is not a way to adopt an existing object.