Skip to main content

Chapter 19 - Terraform Datasources

When resources already exist and don't need to be imported

Data Sources


https://developer.hashicorp.com/terraform/language/data-sources You can use data sources to gather information that exists in Azure.

Examples include:

  • tenant id
  • subscription id
  • subscription spending limit

You can then use these data sources inside your Terraform configuration

Note: you can use the depends_on in the data block to defer the reading of the data block until after the changes to the resources are applied. One use for this is a before/after scenario inside of the terraform run. If you create the resource inside of this terraform, the data block will always fail without a depends_on.

Quick AWS Demo


https://developer.hashicorp.com/terraform/tutorials/configuration-language/data-sources?variants=terraform-workflow%3Acommunity

Existing Resource in Azure


# Make sure this resource group exists in Azure and not in any Terraform. 
data "azurerm_resource_group" "aa123" {
name = "APPAA123_Dev_RG"
}

output "ds_rg_name1" {
value = data.azurerm_resource_group.aa123.name
}

output "ds_rg_location1" {
value = data.azurerm_resource_group.aa123.location
}

output "ds_rg_id1" {
value = data.azurerm_resource_group.aa123.id
}