Home > Enterprise >  Terraform complains parent resource does not exist even though exist
Terraform complains parent resource does not exist even though exist

Time:09-27

I am trying to create some resources in azure with terraform.

What I have:

resource "azurerm_log_analytics_workspace" "logws" {
  name                = lower("log-${var.env}-${local.location_prefix[coalesce(var.location)]}-${random_string.postfix.result}")
  resource_group_name = azurerm_resource_group.rg[0].name
  location            = azurerm_resource_group.rg[0].location
  sku                 = var.log_analytics_workspace_sku
  retention_in_days   = var.log_analytics_logs_retention_in_days
  tags                = local.common_tags
}



resource "azurerm_monitor_private_link_scoped_service" "logscopelink" {
  name                = "scoped-${azurerm_log_analytics_workspace.logws.name}"
  resource_group_name = azurerm_resource_group.rg[0].name
  scope_name          = azurerm_log_analytics_workspace.logws.name
  linked_resource_id = azurerm_log_analytics_workspace.logws.id
  depends_on = [azurerm_log_analytics_workspace.logws]
}

log analytics workspace is created but its when it try to create private_link_scoped_service it fails saying, parent resource not found.

Error I get:

│ Error: creating/updating Private Link Scoped Service: (Scoped Resource Name "scoped-log-sbx-we-oe728m" / Private Link Scope Name "log-sbx-we-oe728m" / Resource Group "hub"): insights.PrivateLinkScopedResourcesClient#CreateOrUpdate: Failure sending request: StatusCode=404 -- Original Error: Code="ParentResourceNotFound" Message="Can not perform requested operation on nested resource. Parent resource 'log-sbx-we-oe728m' not found."

I verified via azure portal, that logws does exist.

Can someone suggest what is wrong here.

CodePudding user response:

You need to create a new azurerm_monitor_private_link_scope resource, then reference it in the scope_name attribute of the azurerm_monitor_private_link_scoped_service resource, example:

resource "azurerm_log_analytics_workspace" "logws" {
  name                = lower("log-${var.env}-${local.location_prefix[coalesce(var.location)]}-${random_string.postfix.result}")
  resource_group_name = azurerm_resource_group.rg[0].name
  location            = azurerm_resource_group.rg[0].location
  sku                 = var.log_analytics_workspace_sku
  retention_in_days   = var.log_analytics_logs_retention_in_days
  tags                = local.common_tags
}

# New resource required
resource "azurerm_monitor_private_link_scope" "example" {
  name                = var.private_link_scope_name
  resource_group_name = azurerm_resource_group.rg[0].name
}

resource "azurerm_monitor_private_link_scoped_service" "logscopelink" {
  name                = "scoped-${azurerm_log_analytics_workspace.logws.name}"
  resource_group_name = azurerm_resource_group.rg[0].name
  scope_name          = azurerm_monitor_private_link_scope.example.name
  linked_resource_id  = azurerm_log_analytics_workspace.logws.id
}

Note that I've removed the explicit depends_on attribute as Terraform can infer on its own the dependencies between resources when you reference an attribute from a resource in another resource block.

  • Related