Azure / terraform

Source code for the Azure Marketplace Terraform development VM package.
MIT License
687 stars 774 forks source link

module data source is not found when running a terraform refresh on a code that is calling a module! #138

Open samanshariq opened 1 year ago

samanshariq commented 1 year ago
          Please open this issue in Terraform repo

Originally posted by @yonzhan in https://github.com/Azure/azure-cli/issues/25271#issuecomment-1407819248

Hello Team, Basically I have run the code given below through the Azure DevOps pipeline already. Got few existing resources so want to import the state file locally . However when I am running the terraform refresh, I get the below error ;

" Error: Route Table: (Name "platform-rt-uks-legacy-prd-zscl" / Resource Group "platform-rg-uks-legacy-prd") was not found

│ with module.landing_zone.data.azurerm_route_table.zscl["uks"], │ on ..\module-tf-landing_zone\networking.tf line 43, in data "azurerm_route_table" "zscl": │ 43: data "azurerm_route_table" "zscl" { " Although the above route exists in the portal.

Here is my code below:

resource "azurerm_route_table" "rt" { for_each = var.Location

name = replace(replace(replace(local.roleCount, "type", "rt"), "role", "lz"), "count", "0${1 + index(keys(var.Location), each.key)}") location = azurerm_resource_group.rg[each.key].location resource_group_name = azurerm_resource_group.rg[each.key].name disable_bgp_route_propagation = false tags = local.tags lifecycle { ignore_changes = [ tags["FirstCreated"] ] } } resource "azurerm_route" "routes" { for_each = local.rt_routes

name = each.value.rt_key resource_group_name = azurerm_resource_group.rg[each.value.loc_key].name route_table_name = azurerm_route_table.rt[each.value.loc_key].name address_prefix = each.value.prefix next_hop_type = each.value.hop_type next_hop_in_ip_address = each.value.region == "UK South" ? local.firewalls.uksouth : local.firewalls.ukwest }

data "azurerm_route_table" "zscl" { provider = azurerm.prd-hub

for_each = local.zscl-rt-table

name = each.value.name resource_group_name = each.value.rg } resource "azurerm_route" "zscl-routes" { provider = azurerm.prd-hub

for_each = local.zscl-routes

name = each.value.loc_key == "Primary" ? replace(local.baseName, "type", "RouteTo") : replace(local.baseNameSecondary, "type", "RouteTo") resource_group_name = data.azurerm_route_table.zscl[each.value.zscl_key].resource_group_name route_table_name = data.azurerm_route_table.zscl[each.value.zscl_key].name address_prefix = each.value.address_space[0] next_hop_type = "VirtualAppliance" next_hop_in_ip_address = each.value.zscl_key == "ukw" ? local.firewalls.ukwest : local.firewalls.uksouth }

lonegunmanb commented 1 year ago

Hi @samanshariq , the error message showed:

" Error: Route Table: (Name "platform-rt-uks-legacy-prd-zscl" / Resource Group "platform-rg-uks-legacy-prd") was not found
│ with module.landing_zone.data.azurerm_route_table.zscl["uks"],
│ on ..\module-tf-landing_zone\networking.tf line 43, in data "azurerm_route_table" "zscl":
│ 43: data "azurerm_route_table" "zscl" {
"

Your Terraform code:

data "azurerm_route_table" "zscl" {
provider = azurerm.prd-hub

for_each = local.zscl-rt-table

name = each.value.name
resource_group_name = each.value.rg
}

There're two potential errors:

  1. Was this provider = azurerm.prd-hub using the right account configuration?
  2. resource_group_name = each.value.rg Was your resource group name platform-rg-uks-legacy-prd right?
samanshariq commented 1 year ago

Hi @samanshariq , the error message showed:

" Error: Route Table: (Name "platform-rt-uks-legacy-prd-zscl" / Resource Group "platform-rg-uks-legacy-prd") was not found
│ with module.landing_zone.data.azurerm_route_table.zscl["uks"],
│ on ..\module-tf-landing_zone\networking.tf line 43, in data "azurerm_route_table" "zscl":
│ 43: data "azurerm_route_table" "zscl" {
"

Your Terraform code:

data "azurerm_route_table" "zscl" {
provider = azurerm.prd-hub

for_each = local.zscl-rt-table

name = each.value.name
resource_group_name = each.value.rg
}

There're two potential errors:

  1. Was this provider = azurerm.prd-hub using the right account configuration?
  2. resource_group_name = each.value.rg Was your resource group name platform-rg-uks-legacy-prd right?

Thanks for coming back to my query. Yes below is the part of code from my locals.tf

" zscl-routes = { for k, v in flatten([ for loc_key, loc in var.Location : [ for zscl_key, zscl in local.zscl-rt-table : {

      loc_key       = loc_key
      zscl_key      = zscl_key
      region        = loc.Region
      address_space = azurerm_virtual_network.vnet[loc_key].address_space
    }
  ]
]) : "${v.loc_key}.${v.zscl_key}" => v

} zscl-rt-table = { uks = { name = "platform-rt-uks-legacy-prd-zscl" rg = "platform-rg-uks-legacy-prd" } ukw = { name = "platform-rt-ukw-legacy-pdr-zscl" rg = "platform-rg-ukw-legacy-pdr" } ncus = { name = "platform-rt-ncus-legacy-prd-zscl" rg = "platform-rg-ncus-prd" } }

rt_routes = { for reg_key, reg in flatten([ for loc_key, loc in var.Location : [ for rt_key, rt in local.routes : {

      loc_key  = loc_key
      rt_key   = rt_key
      prefix   = rt.prefix
      hop_type = rt.hop_type
      region   = loc.Region
    }
  ]
]) : "${reg.loc_key}.${reg.rt_key}" => reg

} }"

samanshariq commented 1 year ago

yes it is using correct provider details:

terraform { required_version = "~> 1.2"

required_providers { azurerm = { source = "hashicorp/azurerm" version = "~> 3.0" configuration_aliases = [azurerm.prd-hub] }

azuread = {
  source                = "hashicorp/azuread"
  version               = "~> 2.0"
  configuration_aliases = [azuread.aad, ]
}

random = {
  source  = "hashicorp/random"
  version = "~> 3.0"
}

} } provider "azurerm" { alias = "prd-hub" subscription_id = var.prd_hub_subscription_id client_id = var.client_id_prd_hub client_secret = var.client_secret_prd_hub

features {} } provider "azuread" { alias = "aad" client_id = var.aad_client_id client_secret = var.aad_client_secret }