使用计数元参数将 public IP 地形分配给 VM
terraform assignment of public IPs to VM's using count meta-argument
我正在尝试构建一个基于计数构建 VM 的脚本。我已经设法让它的大部分工作,但我失败的部分是将创建的 public Ips 分配给 Nic。
public ips 已创建但分配失败,我不确定如何正确解析。代码和错误。
# Create public IPs
resource "azurerm_public_ip" "myterraformpublicip" {
count = 2
name = "myPublicIP-${count.index + 1}"
location = "westeurope"
resource_group_name = azurerm_resource_group.myterraformgroup.name
allocation_method = "Dynamic"
}
# Create network interface
resource "azurerm_network_interface" "myterraformnic" {
count = 2
name = "myNIC-${count.index + 1}"
location = "westeurope"
resource_group_name = azurerm_resource_group.myterraformgroup.name
ip_configuration {
name = "myNicConfiguration"
subnet_id = azurerm_subnet.myterraformsubnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "azurerm_network_interface.myterraformpublicip.[count.index + 1].id"
}
}
#运行计划时的错误。
Error: Can not parse "ip_configuration.0.public_ip_address_id" as a
resource id: Cannot parse Azure ID: parse
"element(azurerm_network_interface.myterraformpublicip.*.id,
count.index + 1)": invalid URI for request
您可以使用如下内容满足您的要求:
provider "azurerm" {
features{}
}
data "azurerm_resource_group" "test" {
name = "yourresourcegroup"
}
resource "azurerm_virtual_network" "vnet" {
name = "ansuman--vnet"
location = data.azurerm_resource_group.test.location
resource_group_name = data.azurerm_resource_group.test.name
address_space = ["10.0.0.0/16"]
}
variable "subnet_prefix" {
type = list
default = [
{
ip = "10.0.1.0/24"
name = "subnet-1"
},
{
ip = "10.0.2.0/24"
name = "subnet-2"
}
]
}
resource "azurerm_subnet" "test_subnet" {
name = "${lookup(element(var.subnet_prefix, count.index), "name")}"
count = "${length(var.subnet_prefix)}"
resource_group_name = data.azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefix = "${lookup(element(var.subnet_prefix, count.index), "ip")}"
}
resource "azurerm_public_ip" "myterraformpublicip" {
count = 2
name = "myPublicIP-${count.index + 1}"
location = data.azurerm_resource_group.test.location
resource_group_name = data.azurerm_resource_group.test.name
allocation_method = "Dynamic"
}
# Create network interface
resource "azurerm_network_interface" "myterraformnic" {
count = 2
name = "myNIC-${count.index + 1}"
location = data.azurerm_resource_group.test.location
resource_group_name = data.azurerm_resource_group.test.name
ip_configuration {
name = "myNicConfiguration"
subnet_id = azurerm_subnet.test_subnet[count.index].id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.myterraformpublicip[count.index].id
}
}
输出:
我正在尝试构建一个基于计数构建 VM 的脚本。我已经设法让它的大部分工作,但我失败的部分是将创建的 public Ips 分配给 Nic。 public ips 已创建但分配失败,我不确定如何正确解析。代码和错误。
# Create public IPs
resource "azurerm_public_ip" "myterraformpublicip" {
count = 2
name = "myPublicIP-${count.index + 1}"
location = "westeurope"
resource_group_name = azurerm_resource_group.myterraformgroup.name
allocation_method = "Dynamic"
}
# Create network interface
resource "azurerm_network_interface" "myterraformnic" {
count = 2
name = "myNIC-${count.index + 1}"
location = "westeurope"
resource_group_name = azurerm_resource_group.myterraformgroup.name
ip_configuration {
name = "myNicConfiguration"
subnet_id = azurerm_subnet.myterraformsubnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "azurerm_network_interface.myterraformpublicip.[count.index + 1].id"
}
}
#运行计划时的错误。
Error: Can not parse "ip_configuration.0.public_ip_address_id" as a resource id: Cannot parse Azure ID: parse "element(azurerm_network_interface.myterraformpublicip.*.id, count.index + 1)": invalid URI for request
您可以使用如下内容满足您的要求:
provider "azurerm" {
features{}
}
data "azurerm_resource_group" "test" {
name = "yourresourcegroup"
}
resource "azurerm_virtual_network" "vnet" {
name = "ansuman--vnet"
location = data.azurerm_resource_group.test.location
resource_group_name = data.azurerm_resource_group.test.name
address_space = ["10.0.0.0/16"]
}
variable "subnet_prefix" {
type = list
default = [
{
ip = "10.0.1.0/24"
name = "subnet-1"
},
{
ip = "10.0.2.0/24"
name = "subnet-2"
}
]
}
resource "azurerm_subnet" "test_subnet" {
name = "${lookup(element(var.subnet_prefix, count.index), "name")}"
count = "${length(var.subnet_prefix)}"
resource_group_name = data.azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefix = "${lookup(element(var.subnet_prefix, count.index), "ip")}"
}
resource "azurerm_public_ip" "myterraformpublicip" {
count = 2
name = "myPublicIP-${count.index + 1}"
location = data.azurerm_resource_group.test.location
resource_group_name = data.azurerm_resource_group.test.name
allocation_method = "Dynamic"
}
# Create network interface
resource "azurerm_network_interface" "myterraformnic" {
count = 2
name = "myNIC-${count.index + 1}"
location = data.azurerm_resource_group.test.location
resource_group_name = data.azurerm_resource_group.test.name
ip_configuration {
name = "myNicConfiguration"
subnet_id = azurerm_subnet.test_subnet[count.index].id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.myterraformpublicip[count.index].id
}
}
输出: