Using outputs from one module for another module in azurerm

Ok.. so..

I have an issue where main.tf calls one module (appservice.tf) which has an output that is then relied upon by another module (roleassignment.tf)

In my case… from main.tf I call my appservice.tf

module "contractreview_apibackendappservice" {
  source = "./appservice"
  app_service_plan_id = module.appserviceplan.app_service_plan_id
  resource_group_name = module.vnet.resource_group_name
  location = var.location
  app_service_name = "zettle-${var.environment_name}-contracts-api"
  subnet_id = module.vnet.defaultsubnet_id
}

appservice.tf code here below, we create a managed identity

resource "azurerm_windows_web_app" "appservice" {
  name                = var.app_service_name
  resource_group_name = var.resource_group_name
  location            = var.location
  service_plan_id     = var.app_service_plan_id
  https_only          = true
  identity {
    type = "SystemAssigned"
  }

  site_config {
    http2_enabled = true
    websockets_enabled = true
    always_on = true
    vnet_route_all_enabled = true
    use_32_bit_worker = false    
  }
  virtual_network_subnet_id = var.subnet_id
}

appservice.tf output variable:

output "principal_id_of_managed_identity" {
  value = azurerm_windows_web_app.appservice.identity[0].principal_id
}

Back in main.tf I reference that variable

locals {
  rows_for_storage_account = [
    {
      role_definition_name = "Storage Blob Data Contributor"
      principal_id = module.contractreview_apibackendappservice.principal_id_of_managed_identity
    },
    {
      role_definition_name = "Storage Queue Data Contributor"
      principal_id = module.contractreview_apibackendappservice.principal_id_of_managed_identity
    }     
  ]
}

module "roleassignment_for_api_storage" {
  source = "./roleassignment"

  for_each = { for row in local.rows_for_storage_account : "${row.role_definition_name}-${row.principal_id}" => row }

  scope = module.storageaccount.storage_account_id
  role_definition_name = each.value.role_definition_name
  principal_id_of_managed_identity_to_give_role_assignment_to = each.value.principal_id
  app_service_name = module.contractreview_apibackendappservice.app_service_name
  depends_on = [ module.contractreview_apibackendappservice ]
}

But it errors when terraform plan runs.. roleassignment module is looking for the value of the principal id that hasn’t been established yet.

│ Error: Invalid template interpolation value

│   on main.tf line 138, in module "roleassignment_for_api_storage":

│  138:   for_each = { for row in local.rows_for_storage_account : "${row.role_definition_name}-${row.principal_id}" => row }

│     │ row.principal_id is null

│ The expression result is null. Cannot include a null value in a string template.

How can I get the roleassignment module to only run once the principal_id_of_managed_identity is established / known?

I know I can do one terraform plan/apply to get the managed principal set up / established and then uncomment the roleassignment module and run a second plan/apply for the role assignment creation but this is obviously a hack of the highest order. This works but clearly sucks.

I guess, I need some sort of way to create a dependency between the two? Currently terraform isn’t recognising the dependency on it’s own or the fact that the principal id is to be established when the apply is run.

I’m using azurerm 3.100.0 if that’s relevant

Thanks 🙂

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật