Thursday, 15 January 2026

Using Remote Terraform State

 Sometimes resources common to multiple diffrent setups might need to be created with a common terraform code. In such cases the commeo terraform resources may need to be reffered with its state in  other terraform code. For this requirement we can use terraform remote state. Let's see how we can use terraform remote state step by step in this post.

The expectation is to refer to the Azure resources in remote terraform state as shown below. Here you can see we have reffered to the resource group name and location, and to log analytics workspace id from remote state.


Let's look at how this resource group and log analytics workspace are defined in the common terraform code and how its details are put to the state first.

Resource Group

resource "azurerm_resource_group" "instancerg" {
  name     = "${var.prefix}-${var.PROJECT}-${var.ENVNAME}-rg"
  location = var.REGION

  tags = merge(tomap({
    Service              = "resource group"
    SystemClassification = local.systemclassification_default_tag
  }), local.tags)
}

Loga analytics workspace

resource "azurerm_log_analytics_workspace" "log_analytics_workspace" {
  name                = "${var.prefix}-${var.PROJECT}-${var.ENVNAME}-log"
  location            = azurerm_resource_group.instancerg.location
  resource_group_name = azurerm_resource_group.instancerg.name
  retention_in_days   = 30

  tags = merge(tomap({
    Service              = "analytics"
    SystemClassification = local.systemclassification_customerdata_tag
  }), local.tags)
}

Outputs are defned so that thay are sotred as outputs in the terraform state file of the common terraform.

#region Outputs for remote state reference
output "instance_rg_id" {
  value = azurerm_resource_group.instancerg.id
}

output "instance_rg_name" {
  value = azurerm_resource_group.instancerg.name
}

output "instance_rg_location" {
  value = azurerm_resource_group.instancerg.location
}

output "loganalyticsworkspace_id" {
  value = azurerm_log_analytics_workspace.log_analytics_workspace.id
}

output "loganalyticsworkspace_name" {
  value = azurerm_log_analytics_workspace.log_analytics_workspace.name
}
#endregion Outputs for remote state reference


Then in our terraform code for specific purpose, we can refer to the remote state file of the common terraform code above.

data "terraform_remote_state" "selfhosted_infra" {
  backend = "azurerm"
  config = {
    storage_account_name = "ch${var.ENV}statecommon"
    container_name       = "${var.ENV}-tfstate"
    key                  = "${var.SH_TFSTATEFILE}" # remote state file name
    resource_group_name  = "ch-iac-state-rg"
    subscription_id      = var.COMMONSUBSCRIPTIONID # Azure subscription of TF state
  }
}

Once we reffered the remote state as above in the terraform code for specific purpose we cen refer to the values in remote terraform state outputs with below syntax.

data.terraform_remote_state.selfhosted_infra.outputs.instance_rg_name

So we can use the remote state values as shown below.


resource "azurerm_user_assigned_identity" "user_assigned_identity" {
  location            = data.terraform_remote_state.selfhosted_infra.outputs.instance_rg_location
  resource_group_name = data.terraform_remote_state.selfhosted_infra.outputs.instance_rg_name
  name                = "${var.prefix}-${var.PROJECT}-${var.ENVNAME}-uai"
}

resource "azurerm_application_insights" "instanceappinsights" {
  name                 = "${var.prefix}-${var.PROJECT}-${var.ENVNAME}-ains"
  location             = data.terraform_remote_state.selfhosted_infra.outputs.instance_rg_location
  resource_group_name  = data.terraform_remote_state.selfhosted_infra.outputs.instance_rg_name
  application_type     = "web"
  daily_data_cap_in_gb = 10
  retention_in_days    = 30
  workspace_id         = data.terraform_remote_state.selfhosted_infra.outputs.loganalyticsworkspace_id

  tags = merge(tomap({
    Service              = "application insights"
    SystemClassification = local.systemclassification_customerdata_tag
  }), local.tags)
}

No comments:

Popular Posts