簡體   English   中英

Terraform for_each - each.key 在哪里定義?

[英]Terraform for_each - where is each.key defined?

預期行為

我試圖更好地理解 for_each 循環

具體來說,模塊調用中定義的each.key值在哪里?
部署 1 個實例的示例是什么? 然后是多個實例的示例?
請參閱下面的“模塊調用(父模塊)”

實際行為

不確定預期結果

Terraform(和 AzureRM 提供程序)版本

受影響的資源

  • azurerm_v2.41.0
  • terraform v0.13.0

Terraform 配置文件

主文件

resource "azurerm_resource_group_template_deployment" "sql_mi" {
  for_each            = var.sql_mi_defaults
  
  name                = each.key # provide an example of how this is defined in module call?  # how would I deploy more than one instance?
  resource_group_name = var.resource_group_name
  deployment_mode     = var.deployment_mode
  template_content    = data.local_file.arm_template.content
  parameters_content  = <<PARAMETERS
     {
        "managedInstanceName": {
            "value": "${each.value.managedInstanceName}" 
        },
        "location": {
            "value": "${var.location}"
        },
        "skuName": {
            "value": "${each.value.skuName}"
        },
        "storageSizeInGB": {
            "value": "${each.value.storageSizeInGB}"
        },
        "vCores": {
            "value": "${each.value.vCores}"
        },
        "licenseType": {
            "value": "${each.value.licenseType}"
        },
        "collation": {
            "value": "${each.value.collation}"
        },
        "timezoneId": {
            "value": "${each.value.timezoneId}"
        },
        "collation": {
            "value": "${each.value.collation}"
        },
        "proxyOverride": {
            "value": "${each.value.proxyOverride}"
        },
        "publicDataEndpointEnabled": {
            "value": "${each.value.publicDataEndpointEnabled}"
        },
        "administratorLogin": {
            "value": "azadmin-${random_string.mi_name.result}"
        },
        "administratorLoginPassword": {
            "value": "${random_password.admin.result}"
        },
        "managedInstanceTags": {
            "value": "${var.names.product_name}-${var.names.service_name}-${random_string.mi_name.result}"
        },
        "storageAccountType": {
            "value": "${each.value.storageAccountType}"
        },
        "virtualNetworkName": {
            "value": "${var.virtual_network_name}"
        },
        "virtualNetworkResourceGroupName": {
            "value": "${var.resource_group_name}"
        },
    }
    PARAMETERS
}

變量.tf

variable sql_mi_defaults {
  type = any
  default = {
    managedInstanceName        = "randomcomputername"
    location                   = "eastus2"
    skuName                    = "Standard_F2"
    storageSizeInGB            = 256
    vCores                     = 8
    licenseType                = "LicenseIncluded"
    collation                  = "SQL_Latin1_General_CP1_CI_AS"
    timezoneId                 = "UTC"
    proxyOverride              = "Proxy"
    publicDataEndpointEnabled  = false
    minimalTlsVersion          = "1.2"
    administratorLogin         = "azadmin"
    administratorLoginPassword = ""
    managedInstanceTags        = ""
    storageAccountType         = "GRS"

  }
  description = <<EOT
azure sql managed instance settings (only applied to virtual machine settings managed within this module)
    managedInstanceName             = (Required) The name of the Managed Instance.
    location                        = (Required) The location of the Managed Instance
    skuName                         = (Required) Managed instance SKU. If SKU is not set, skuEdition and hardwareFamily values have to be populated."
    storageSizeInGB                 = (Required) Determines how much Storage size in GB to associate with instance. Increments of 32 GB allowed only.
    vCores                          = (Required) The number of vCores.
    licenseType                     = (Optional) Determines license pricing model. Select 'LicenseIncluded' for a regular price inclusive of a new SQL license. Select 'Base Price' for a discounted AHB price for bringing your own SQL licenses.
    collation                       = (Optional) Specifies the priority of this Virtual Machine. Possible values are Regular and Spot. Defaults to Regular. Changing this forces a new resource to be created.
    timezoneId                      = (Optional) Specifies what should happen when the Virtual Machine is evicted for price reasons when using a Spot instance. At this time the only supported value is Deallocate. Changing this forces a new resource to be created. This can only be configured when priority is set to Spot.
    proxyOverride                   = (Optional) Determines connection type for private endpoint. Proxy connection type enables proxy connectivity to Managed Instance. Redirect mode enables direct connectivity to the instance resulting in improved latency and throughput.
    publicDataEndpointEnabled       = (Optional) Determines whether public data endpoint will be enabled, required for clients outside of the connected virtual networks. Public endpoint will always default to Proxy connection mode.
    administratorLogin              = (Required) The login of the Managed Instance admin.
    administratorLoginPassword      = (Required) The password of the Managed Instance admin.
    managedInstanceTags             = (Optional) Resource tags to associate with the instance.
    storageAccountType              = (Required) Option for configuring backup storage redundancy. Selecting 'GRS' will enable 'RA-GRS'.
    virtualNetworkName              = (Required) The virtual network name. Leave empty for the default value.
    virtualNetworkResourceGroupName = (Required) The resource group where the networking resources will be created or updated. Default is the same resource group as Managed Instance.
EOT 
}

模塊調用(父模塊)

# azurerm_sql_managed_instance see for more info https://docs.microsoft.com/en-us/azure/azure-sql/managed-instance/sql-managed-instance-paas-overview
module "sql_mi" {
  source = "github.com/[redacted]/azurerm-sql-managed-instance.git?ref=v2.3.1""

  resource_group_name  = module.resource_group.name
  location             = module.resource_group.location
  deployment_mode      = "Complete"
  virtual_network_name = module.virtual_network.subnet_nsg_names["iaas-outbound"]

  name = "example-output-from-each.key" # will the name of the instance be from each.key?
}

這個:

for_each    = var.sql_mi_defaults

告訴 Terraform 獲取 var.sql_mi_defaults 中的任何集合,並azurerm_resource_group_template_deployment var.sql_mi_defaults 它會自動公開資源塊的 scope 中的each.keyeach.value屬性。 這都記錄在這里


部署 1 個實例的示例是什么?

將大小為 1 的集合作為var.sql_mi_defaults

然后是多個實例的示例?

將大小 > 1 的集合作為var.sql_mi_defaults

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM