terraform:仅将 public IP 添加到一个 azure VM
terraform: add public IP to only one azure VM
我正在 azurerm_virtual_machine 中通过计数创建 4 个虚拟机,但我只想创建一个 public IP 并将其与第一个虚拟机相关联?这可能吗?
下面是我的模板文件
resource "azurerm_network_interface" "nics" {
count = 4
name = ...
location = ...
resource_group_name = ...
ip_configuration {
subnet_id = ...
private_ip_address_allocation = "Static"
private_ip_address = ...
}
}
resource "azurerm_public_ip" "public_ip" {
name = ...
location = ...
resource_group_name = ...
}
resource "azurerm_virtual_machine" "vms" {
count = 4
network_interface_ids = [element(azurerm_network_interface.nics.*.id, count.index)]
}
我已经解决了以下问题,但他们创建了多个 public ip 并将它们添加到所有 vms。
Public IP 是使用 azurerm_public_ip:
创建的
resource "azurerm_public_ip" "public_ip" {
name = "acceptanceTestPublicIp1"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
allocation_method = "Dynamic"
}
在您的 azurerm_network_interface
中有了地址,您可以使用 Conditional Expressions 执行以下操作:
resource "azurerm_network_interface" "nics" {
count = 4
name = ...
location = ...
resource_group_name = ...
ip_configuration {
subnet_id = ...
private_ip_address_allocation = "Static"
private_ip_address = ...
public_ip_address_id = count.index == 1 ? azurerm_public_ip.public_ip.id : null
}
}
我正在 azurerm_virtual_machine 中通过计数创建 4 个虚拟机,但我只想创建一个 public IP 并将其与第一个虚拟机相关联?这可能吗?
下面是我的模板文件
resource "azurerm_network_interface" "nics" {
count = 4
name = ...
location = ...
resource_group_name = ...
ip_configuration {
subnet_id = ...
private_ip_address_allocation = "Static"
private_ip_address = ...
}
}
resource "azurerm_public_ip" "public_ip" {
name = ...
location = ...
resource_group_name = ...
}
resource "azurerm_virtual_machine" "vms" {
count = 4
network_interface_ids = [element(azurerm_network_interface.nics.*.id, count.index)]
}
我已经解决了以下问题,但他们创建了多个 public ip 并将它们添加到所有 vms。
Public IP 是使用 azurerm_public_ip:
创建的resource "azurerm_public_ip" "public_ip" {
name = "acceptanceTestPublicIp1"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
allocation_method = "Dynamic"
}
在您的 azurerm_network_interface
中有了地址,您可以使用 Conditional Expressions 执行以下操作:
resource "azurerm_network_interface" "nics" {
count = 4
name = ...
location = ...
resource_group_name = ...
ip_configuration {
subnet_id = ...
private_ip_address_allocation = "Static"
private_ip_address = ...
public_ip_address_id = count.index == 1 ? azurerm_public_ip.public_ip.id : null
}
}