amazon-web-services - Terraform for_each - each.key 在哪里定义?
问题描述
预期行为
我试图更好地理解 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
资源。它会自动公开资源块范围内的each.key
和属性。each.value
这都记录在这里。
部署 1 个实例的示例是什么?
将大小为 1 的集合传递为var.sql_mi_defaults
然后是多个实例的示例?
将大小 > 1 的集合传递为var.sql_mi_defaults
推荐阅读
- sql-server - 如何在 SSIS for Oracle 源中默认将 DT_STR 更改为 DT_WSTR
- java - NoSuchMethodError 与 spring-webmvc 升级
- multithreading - 在 Kotlin Native 中,如何在不使用 C 指针的情况下将对象保留在单独的线程中,并从任何其他线程中改变其状态?
- android - Android ViewPager2、Webview、全屏和触摸事件
- javascript - 限制javascript节点js中的并发性
- java - Spring IntegrationFlow 实现中的 ClassCastException
- c# - 正则表达式 - 匹配特定字符(多次)并忽略注释
- javascript - 如何处理快速会话?
- ruby-on-rails - 在 Rails 5 中重命名关联
- java - spring gateway:如何根据请求头和路径动态设置 URI 和路径