Day 32: Mastering Terraform Data Sources

Adrian Rubico

|

Apr 23, 2025

09:12 PM GMT+8

Day 32: Mastering Terraform Data Source

Terraform Data Sources allow you to fetch and reference existing infrastructure in your configuration. This is helpful when you need to work with infrastructure not directly created by your current Terraform project. Data sources provide a dynamic and reusable way to retrieve information about existing resources and integrate them into your infrastructure plans.

Difference between Terraform Data Sources, Resource and Variable

Terraform state is essential because it is a critical component that tracks the current state of the infrastructure as defined in the Terraform configuration. It serves several key purposes:

  • Resource: Used to provision infrastructure on your platform. It handles the creation, updating, and deletion of infrastructure components.
  • Variable: Stores predefined values that can be referenced throughout your Infrastructure as Code (IaC). These values help resources remain flexible and reusable.
  • Data Source: Used to retrieve information from existing infrastructure or providers. It allows you to reference existing resources without managing their lifecycle.

Without state, Terraform would have to query the entire infrastructure, which is inefficient and error-prone.

Task

Prerequisite:

1️⃣ Complete the task from Day 31, which provisions a resource group and a virtual machine. These existing resources are essential because we will reference them in this task.

2️⃣ If you have not completed Day 31, you can manually create a resource group named rg-example to follow along with this exercise.

3️⃣ Clone the following repository and navigate to the correct directory:

bash
https://github.com/git-adrianrubico/learn-terraform/tree/master
cd learn-terraform/03-Provisioner

Problem Overview

Open the main.tf file. You will see the following configuration.

hcl
resource "azurerm_storage_account" "sa-example" {
  name                     = "stgacsample-dev01"
  resource_group_name      = azurerm_resource_group.rg-example.name
  location                 = azurerm_resource_group.rg-example.location
  account_tier             = "Standard"
  account_replication_type = "GRS"
  tags = {
    environment = azurerm_resource_group.rg-example.location
  }
}

This configuration is incomplete. The azurerm_resource_group block is missing, which causes an error. Since this folder is part of a new project, it does not have access to the Terraform state file (tfstate) from the previous task. Therefore, the storage account resource cannot reference a non-existent resource group

Solution Using Data Sources

We will now modify the code to use a data source instead of referencing a non-existent local resource. This allows us to access the details of an existing resource group.

Update your main.tf as follows:

hcl
data "azurerm_resource_group" "exist-rg-example" {
  name     = "rg-example"
}

resource "azurerm_storage_account" "sa-example" {
  name                     = "stgacsample-dev01"
  resource_group_name      = data.azurerm_resource_group.exist-rg-example.name
  location                 = data.azurerm_resource_group.exist-rg-example.location
  account_tier             = "Standard"
  account_replication_type = "GRS"
  tags = {
    environment = data.azurerm_resource_group.exist-rg-example.location
  }
}

This version uses a data source to retrieve details about an existing resource group. It eliminates the need to recreate resources or manually input values. In this example, only one new resource was added, and we used the data source to reference what already exists.

Use terraform data source

Conclusion

Today, we explored how Terraform Data Sources allow you to dynamically access existing resources and integrate them into a new configuration. Using data sources helps create a clean and maintainable setup by referencing existing infrastructure without needing to manage it directly.

In the next blog, we will continue by diving deeper into Terraform HCL features to enhance infrastructure code readability and efficiency.

Discussion