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
Loga analytics workspace
Outputs are defned so that thay are sotred as outputs in the terraform state file of the common terraform.
Then in our terraform code for specific purpose, we can refer to the remote state file of the common terraform code above.
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.
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:
Post a Comment