0

我有下面的 TF 文件,它将创建一个函数 - FirstFunction。这完美地工作。

resource "azurerm_function_app" "**firstfunction**" {
  name                = **var.firstfunctionname**
  location            = azurerm_resource_group.resourcegroupX.location
  resource_group_name = azurerm_resource_group.resourcegroupX.name
  app_service_plan_id = azurerm_app_service_plan.appserviceplan.id
  https_only          = "true"
  client_affinity_enabled = "true"
  
  app_settings = {   
    NS                     = azurerm_eventhub_namespace.eventhubns.name
    Hub                    = azurerm_eventhub.**firsteventhub**.name    
    propertyX       = "**firstproperty**"
    LogRef                       = "${azurerm_storage_account.store.primary_blob_endpoint}${azurerm_storage_container.**firstlogs**.name}"
 }
 }
 
 resource "azurerm_app_service_virtual_network_swift_connection" "**firstvnet**" {
  app_service_id = azurerm_function_app.**firstfunction**.id
  subnet_id      = azurerm_subnet.snet.id

}

在文件中,看到用****括起来的部分,需要更改以创建SecondFunction,ThirdFunction等...

我现在的方法是创建多个 TF 文件,复制相同的代码并更改 ** 中包含的部分。

我通读了模块系统,但了解模块系统的限制是我无法引用在同一 TF 根模块中创建的其他组件,如下所示例如在 TF 文件中,我将位置称为 location = azurerm_resource_group.resourcegroupX .location 如果我将其作为一个模块进行,则该位置应称为 location = var.location_name 其中 location_name 应定义为变量。我不能引用使用相同根模块创建的组件。

您能否提出一个解决方案,让我可以根据类似的代码创建多个组件?请注意,在上面的示例中,我在单个 TF 文件中创建了 2 个资源,并且它们都是相关的。

4

1 回答 1

0

最简单的方法是在资源中使用 count 属性。它可以帮助您在同一代码中创建多个相同的资源。

resource "azurerm_function_app" "myfunction" {
  count               = number_your_need     # how many resources you want to create 
  name                = "${var.firstfunctionname}-${count.index}" 
  location            = azurerm_resource_group.resourcegroupX.location
  resource_group_name = azurerm_resource_group.resourcegroupX.name
  app_service_plan_id = azurerm_app_service_plan.appserviceplan.id
  https_only          = "true"
  client_affinity_enabled = "true"
  
  app_settings = {   
    NS                     = azurerm_eventhub_namespace.eventhubns.name
    Hub                    = azurerm_eventhub.**firsteventhub**.name   
    propertyX       = "**firstproperty**"
    LogRef                       = "${azurerm_storage_account.store.primary_blob_endpoint}${azurerm_storage_container.**firstlogs**.name}"
   }
 }
 
 resource "azurerm_app_service_virtual_network_swift_connection" "**firstvnet**" {
  count          = number      # how many you need to create
  app_service_id = element(azurerm_function_app.myfunction[*].id, count.index)
  subnet_id      = azurerm_subnet.snet.id

}

azurerm_eventhub您需要创建的相同解决方案。例如:

resource "azurerm_eventhub" "myeventhub" {
  count  = number    # how many you need to create 
  name   = "${var.eventhub_name}-${count.index}"
  ...
}

然后你可以像这样在函数应用程序中引用它:

app_settings = {   
    NS                     = azurerm_eventhub_namespace.eventhubns.name
    Hub                    = element(azurerm_eventhub.myeventhub[*].name, count.index)  
    propertyX       = "**firstproperty**"
    LogRef                       = "${azurerm_storage_account.store.primary_blob_endpoint}${azurerm_storage_container.**firstlogs**.name}"
 }

用****括起来的所有部分也是如此。

于 2020-08-04T09:01:17.287 回答