[英]Unable to attach Public IP to Azure windows VM (created using for_each) in Terraform
I use terraform version: v0.14.6 I have been trying to attach a public IP to a Azure windows VM that is being created by for_each condition in terraform. I use terraform version: v0.14.6 I have been trying to attach a public IP to a Azure windows VM that is being created by for_each condition in terraform. I can't figure out how to attach the public IP that is created on a different module again using for_each to my module for Windows VM creation.
我不知道如何将使用 for_each 再次在不同模块上创建的公共 IP 附加到我的模块以创建 Windows VM。 Below is the script I am using:
以下是我正在使用的脚本:
data "azurerm_resource_group" "this" {
count = local.resourcegroup_state_exists == false ? 1 : 0
name = var.resource_group_name
}
data "azurerm_subnet" "this" {
for_each = var.windows_vm_nics
name = each.value.subnet_name
virtual_network_name = each.value.vnet_name
resource_group_name = var.resource_group_name
}
resource "azurerm_public_ip" "public_ip" {
for_each = var.publicIp_variable
name = each.value["name"]
ip_version = each.value["ip_version"]
resource_group_name = var.resource_group_name
location = var.resource_group_location
allocation_method = each.value["allocation"]
sku = each.value["sku"]
domain_name_label = each.value["public_ip_dns"]
idle_timeout_in_minutes = each.value["idle_timeout_in_minutes"]
zones = each.value["zone"]
tags = var.publicIp_tags
}
locals {
resourcegroup_state_exists = false
public_ip_exists ={ for k, v in var.windows_vm_nics : k => v if lookup(v, "use_existing_public_ip ", false) == true }
}
resource "azurerm_network_interface" "windows_nics" {
for_each = var.windows_vm_nics
name = each.value.name
resource_group_name = local.resourcegroup_state_exists == true ? var.resource_group_name : data.azurerm_resource_group.this.0.name
location = local.resourcegroup_state_exists == true ? var.resource_group_name : data.azurerm_resource_group.this.0.location
internal_dns_name_label = lookup(each.value, "internal_dns_name_label", null) #integration testing needs to be done once DNS is created
enable_ip_forwarding = lookup(each.value, "enable_ip_forwarding", null)
enable_accelerated_networking = lookup(each.value, "enable_accelerated_networking", null)
dns_servers = lookup(each.value, "dns_servers", null) #integration testing needs to be done once DNS is created
dynamic "ip_configuration" {
for_each = coalesce(each.value.nic_ip_configurations, [])
content {
name = coalesce(ip_configuration.value.name, format("%s00%d-ip", each.value.name, index(each.value.nic_ip_configurations, ip_configuration.value) + 1))
subnet_id = lookup(data.azurerm_subnet.this, each.key)["id"]
private_ip_address_allocation = lookup(ip_configuration.value, "static_ip", null) == null ? "dynamic" : "static"
private_ip_address = lookup(ip_configuration.value, "static_ip", null)
public_ip_address_id = lookup(each.value, "use_existing_public_ip", false) == true ? lookup(azurerm_public_ip.public_ip[each.value["existing_public_ip_name"]],each.key)["id"] :null #[each.value["existing_public_ip_name"]]
primary = index(each.value.nic_ip_configurations, ip_configuration.value) == 0 ? true : false
}
}
tags = local.tags
depends_on = [ azurerm_public_ip.public_ip ]
}
the below is the variable file I have created for the same:以下是我为其创建的变量文件:
variable "windows_virtual_machine" {
type = map(object({
name = string
vm_size = string
zone = string
assign_identity = bool
availability_set_key = string
vm_nic_keys = list(string)
source_image_reference_publisher = string
source_image_reference_offer = string
source_image_reference_sku = string
source_image_reference_version = string
os_disk_name = string
storage_os_disk_caching = string
storage_account_type = string
disk_size_gb = number
write_accelerator_enabled = bool
license_type = string
ultra_ssd_enabled = bool
custom_data_path = string
custom_data_args = map(string)
}))
description = "Map containing Windows VM objects"
default = {}
}
I believe public_ip_address_id = lookup(each.value, "use_existing_public_ip", false) == true? lookup(azurerm_public_ip.public_ip[each.value["existing_public_ip_name"]],each.key)["id"]:null #[each.value["existing_public_ip_name"]]
我相信
public_ip_address_id = lookup(each.value, "use_existing_public_ip", false) == true? lookup(azurerm_public_ip.public_ip[each.value["existing_public_ip_name"]],each.key)["id"]:null #[each.value["existing_public_ip_name"]]
public_ip_address_id = lookup(each.value, "use_existing_public_ip", false) == true? lookup(azurerm_public_ip.public_ip[each.value["existing_public_ip_name"]],each.key)["id"]:null #[each.value["existing_public_ip_name"]]
is where I need to make the modification but not quite sure what is going wrong. public_ip_address_id = lookup(each.value, "use_existing_public_ip", false) == true? lookup(azurerm_public_ip.public_ip[each.value["existing_public_ip_name"]],each.key)["id"]:null #[each.value["existing_public_ip_name"]]
是我需要修改但不太确定的地方出了什么问题。 Terraform doesn't throw any errors while running the script but it is not attaching the public IP either. Terraform 在运行脚本时不会抛出任何错误,但它也没有附加公共 IP。
Below is the variable file for nic:下面是 nic 的变量文件:
variable "windows_vm_nics" {
type = map(object({
name = string
subnet_name = string
vnet_name = string
networking_resource_group = string
internal_dns_name_label = string
enable_ip_forwarding = bool
enable_accelerated_networking = bool
use_existing_public_ip = bool
existing_public_ip_name = string
existing_public_ip_rg_name = string
dns_servers = list(string)
nic_ip_configurations = list(object({
name = string
static_ip = string
}))
}))
description = "Map containing Windows VM NIC objects"
default = {}
}
I am getting the following error:我收到以下错误:
public_ip_address_id = lookup(each.value, "use_existing_public_ip", false) == true ? lookup(azurerm_public_ip.public_ip[each.value["existing_public_ip_name"]],each.key)["id"] :null #[each.value["existing_public_ip_name"]]
|----------------
| azurerm_public_ip.public_ip is object with 1 attribute "public_IP_1"
| each.value["existing_public_ip_name"] is "Windows-PIP"
The given key does not identify an element in this collection value.给定的键不标识此集合值中的元素。
The below is the tfvars I have used:以下是我使用过的 tfvar:
resource_group_name = "rg-terraform"
resource_group_location = "East US"
publicIp_variable = {
"public_IP_1" = {
name = "Windows-PIP"
ip_version = "IPv4"
allocation = "Static"
sku = "Standard"
public_ip_dns = null
idle_timeout_in_minutes = null
zone = null
}
}
windows_vm_nics = {
nic1 = {
name = "neudesicdemovm01-nic1"
subnet_name = "subnet1"
vnet_name = "terraform_vnet"
networking_resource_group = null
use_existing_public_ip = true
existing_public_ip_name ="Windows-PIP"
existing_public_ip_rg_name ="rg-terraform"
internal_dns_name_label = null
enable_ip_forwarding = null
enable_accelerated_networking = null
dns_servers = null
nic_ip_configurations = [
{
static_ip = null
name = "ip-config"
}
]
}
}
I found that you have public_ip_address_id = lookup(each.value, "use_existing_public_ip", false) == true? lookup(data.azurerm_public_ip.this, each.key)["id"]
我发现你有
public_ip_address_id = lookup(each.value, "use_existing_public_ip", false) == true? lookup(data.azurerm_public_ip.this, each.key)["id"]
public_ip_address_id = lookup(each.value, "use_existing_public_ip", false) == true? lookup(data.azurerm_public_ip.this, each.key)["id"]
in the resource "azurerm_network_interface" "windows_nics"
with for_each = var.windows_vm_nics
.使用
for_each = var.windows_vm_nics
在resource "azurerm_network_interface" "windows_nics"
中public_ip_address_id = lookup(each.value, "use_existing_public_ip", false) == true? lookup(data.azurerm_public_ip.this, each.key)["id"]
。 However, there are no use_existing_public_ip
attibutes in the variable "windows_vm_nics"
.但是,
variable "windows_vm_nics"
windows_vm_nics" 中没有use_existing_public_ip
属性。 You may try to add it.您可以尝试添加它。
For example:例如:
variable "windows_vm_nics" {
type = map(object({
name = string
subnet_name = string
vnet_name = string
networking_resource_group = string
internal_dns_name_label = string
enable_ip_forwarding = bool
enable_accelerated_networking = bool
dns_servers = list(string)
use_existing_public_ip = bool # add this
nic_ip_configurations = list(object({
name = string
static_ip = string
}))
}))
description = "Map containing Windows VM NIC objects"
default = {}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.