在Terraform中切换Azure VM的创建

问题描述 投票:0回答:1

我必须使用Azureterraform中预配Windows VM,唯一的条件是,应为DEV以外的所有其他环境创建VM(以及相关资源,例如VNET,NSG,PublicIP等)。 >

如果我使用以下Terraform代码运行terraform plan。我收到此错误。

##[error]Terraform command 'plan' failed with exit code '1'.:  Missing resource instance key |  Missing resource instance key |  Missing resource instance key

[0m  on main_infra_app.tf line 284, in resource "azurerm_network_interface" "network-interface":
284:         subnet_id                     = "${[4mazurerm_subnet.snet[0m.id}"
[0m
Because azurerm_subnet.snet has "count" set, its attributes must be accessed
on specific instances.
For example, to correlate with indices of a referring resource, use:
azurerm_subnet.snet[count.index]

Terraform代码:

resource "azurerm_virtual_network" "vnet-main" {
    count               = "${var.env == "dev" ? 0 : 1}"
    name                = "$var.name"
    address_space       = ["10.0.0.0/16"]
    location            = "$var.location"
    resource_group_name = "${azurerm_resource_group.rg.name}"
} 

#Create Public IPs
resource "azurerm_public_ip" "PublicIP" {
    count                        = "${var.env  == "dev" ? 0 : 1}"
    name                         = "${var.ip}"
    location                     = "${azurerm_resource_group.rg.location}"
    resource_group_name          = "${azurerm_resource_group.rg.name}"
    allocation_method            = "Static"
}

#Create Subnet
resource "azurerm_subnet" "snet" {
    count                = "${var.env == "dev" ? 0 : 1}"
    name                 = "${var.subnet}"
    resource_group_name  = "${azurerm_resource_group.rg.name}"
    virtual_network_name = "azurerm_virtual_network.vnet-main"
    address_prefix       = "10.0.2.0/24"
}

#Create Network Security Group
resource "azurerm_network_security_group" "NSG" {
    count               = "${var.env == "dev" ? 0 : 1}"
    name                = "${var.nsg}"
    location            = "${azurerm_resource_group.rg.location}"
    resource_group_name = "${azurerm_resource_group.rg.name}"

  security_rule  {
  ...
    }
  security_rule {
  ...
    }
}

#Create Network Interface
resource "azurerm_network_interface" "network-interface" {
    count                     = "${var.env == "dev" ? 0 : 1}"
    name                      = "${var.nic}"
    location                  = "${azurerm_resource_group.rg.location}"
    resource_group_name       = "${azurerm_resource_group.rg.name}"
    network_security_group_id = "${var.devops_stage == "dev" ? azurerm_network_security_group.NSG[count.index] : azurerm_network_security_group.NSG.id}"

    ip_configuration {
        name                          = "IP-Conf-1"
        subnet_id                     = "${var.devops_stage == "dev" ? azurerm_subnet.snet[count.index] : azurerm_subnet.snet.id}"
        private_ip_address_allocation = "Dynamic"
        public_ip_address_id           = "${var.devops_stage == "dev" ? azurerm_public_ip.PublicIP[count.index] : azurerm_public_ip.PublicIP.id}"
   }
}

resource "azurerm_virtual_machine" "vm" {
    count               = "${var.env == "dev" ? 0 : 1}"
    name                  = var.vm_name
    location              = "${azurerm_resource_group.rg.location}"
    resource_group_name   = "${azurerm_resource_group.rg.name}"
    network_interface_ids = "${var.env == "dev" ? azurerm_network_interface.network-interface[count.index] : azurerm_network_interface.network-interface[count.index]}"
    vm_size               = "Standard_D13_v2"
    ..
    ..
    ..
}

请指导我。

我必须使用terraform在Azure中配置Windows VM,只有条件是,应为...之外的所有其他环境创建VM(以及相关资源,例如VNET,NSG,PublicIP等)]

azure-virtual-machine terraform-provider-azure
1个回答
0
投票

正如从您的Terraform代码中看到的那样,您想要添加条件,如果变量env与“ dev”匹配以判断是否创建VM和其他资源。我认为您只需要在条件上添加条件,而无需在其他任何地方添加条件。所以最后,Terraform代码应如下所示:

resource "azurerm_virtual_network" "vnet-main" {
    count               = "${var.env == "dev" ? 0 : 1}"
    name                = "${var.name}"
    address_space       = ["10.0.0.0/16"]
    location            = "${azurerm_resource_group.rg.location}"
    resource_group_name = "${azurerm_resource_group.rg.name}"
} 

#Create Public IPs
resource "azurerm_public_ip" "PublicIP" {
    count                        = "${var.env  == "dev" ? 0 : 1}"
    name                         = "${var.ip}"
    location                     = "${azurerm_resource_group.rg.location}"
    resource_group_name          = "${azurerm_resource_group.rg.name}"
    allocation_method            = "Static"
}

#Create Subnet
resource "azurerm_subnet" "snet" {
    count                = "${var.env == "dev" ? 0 : 1}"
    name                 = "${var.subnet}"
    resource_group_name  = "${azurerm_resource_group.rg.name}"
    virtual_network_name = "${azurerm_virtual_network.vnet-main[count.index].name}"
    address_prefix       = "10.0.2.0/24"
}

#Create Network Security Group
resource "azurerm_network_security_group" "NSG" {
    count               = "${var.env == "dev" ? 0 : 1}"
    name                = "${var.nsg}"
    location            = "${azurerm_resource_group.rg.location}"
    resource_group_name = "${azurerm_resource_group.rg.name}"

  security_rule  {
  ...
    }
  security_rule {
  ...
    }
}

#Create Network Interface
resource "azurerm_network_interface" "network-interface" {
    count                     = "${var.env == "dev" ? 0 : 1}"
    name                      = "${var.nic}"
    location                  = "${azurerm_resource_group.rg.location}"
    resource_group_name       = "${azurerm_resource_group.rg.name}"
    network_security_group_id = "${azurerm_network_security_group.NSG[count.index].id}"

    ip_configuration {
        name                          = "IP-Conf-1"
        subnet_id                     = "${azurerm_subnet.snet[count.index].id}"
        private_ip_address_allocation = "Dynamic"
        public_ip_address_id           = "${azurerm_public_ip.PublicIP[count.index].id}"
   }
}

resource "azurerm_virtual_machine" "vm" {
    count               = "${var.env == "dev" ? 0 : 1}"
    name                  = var.vm_name
    location              = "${azurerm_resource_group.rg.location}"
    resource_group_name   = "${azurerm_resource_group.rg.name}"
    network_interface_ids = "${azurerm_network_interface.network-interface[count.index].id}"
    vm_size               = "Standard_D13_v2"
    ..
    ..
    ..
}
© www.soinside.com 2019 - 2024. All rights reserved.