尝试在azure中构建多个VM时,Terraform Module失败

时间:2019-09-17 02:28:52

标签: azure terraform

我有一个main.tf,variable.tf,可以使用源代码从名为vms.tf的/ module文件中对其进行引用         ../.

但是,如果我尝试构建多个VM,则第二个VM会不断抛出如下错误:         错误:缺少必填参数

    on vms.tf line 6, in module "bar":
    6: module "bar" {

    The argument "client_id" is required, but no definition was found.

如果我仅构建一个虚拟机,则第一个虚拟机将运行良好,但是当我向模块中添加更多虚拟机时,它的行为就像没有凭据一样。除了环境变量,我尝试过将它们直接添加到提供程序下,没有骰子。

我的调用模块如下所示,它本身位于一个文件夹中:

        module "foo" {
            source   = "../."
            location = "westus"
            prefix   = "skachar"
        }
        module "bar" {
            source   = "../."
            prefix   = "skachar2"
            location = "eastus"
        }   

我根据注释部分中某人的要求添加了以下代码。它是我的main.tf文件,位于我的模块所在的文件夹下。我也将添加我的variable.tf文件,只是为了更全面。它将位于main.tf文件下面,该文件是您在本段下面看到的代码。我真的很想知道为什么第二台VM炸毁了。我为其提供了一个唯一的前缀,并且错误消息毫无意义。如果没有凭据,则不会配置第一个VM。任何人,如果有人可以指出我正确的方向,也许我在某处缺少一些文档。感谢您提供的任何帮助。

provider "azurerm" {
  version = "~>1.28.0"
  # subscription_id = "${var.subscription_id}"
  # client_id       = "${var.client_id}"
  # client_secret   = "${var.client_secret}"
  # tenant_id       = "${var.tenant_id}"

}

# Create a resource group
resource "azurerm_resource_group" "rg" {
  name     = "${var.prefix}TFRG"
  location = "${var.location}"
  tags     = "${var.tags}"
}

# Create virtual network
resource "azurerm_virtual_network" "vnet" {
  name                = "${var.prefix}TFVnet"
  address_space       = ["10.0.0.0/16"]
  location            = "${var.location}"
  resource_group_name = "${azurerm_resource_group.rg.name}"
  tags                = "${var.tags}"
}

# Create subnet
resource "azurerm_subnet" "subnet" {
  name                 = "${var.prefix}TFSubnet"
  resource_group_name  = "${azurerm_resource_group.rg.name}"
  virtual_network_name = "${azurerm_virtual_network.vnet.name}"
  address_prefix       = "10.0.1.0/24"
}

# Create public IP
resource "azurerm_public_ip" "publicip" {
  name                         = "${var.prefix}TFPublicIP"
  location                     = "${var.location}"
  resource_group_name          = "${azurerm_resource_group.rg.name}"
  public_ip_address_allocation = "dynamic"
  tags                         = "${var.tags}"
}

# Create Network Security Group and rule
resource "azurerm_network_security_group" "nsg" {
  name                = "${var.prefix}TFNSG"
  location            = "${var.location}"
  resource_group_name = "${azurerm_resource_group.rg.name}"
  tags                = "${var.tags}"

  security_rule {
    name                       = "SSH"
    priority                   = 1001
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

# Create network interface
resource "azurerm_network_interface" "nic" {
  name                      = "${var.prefix}NIC"
  location                  = "${var.location}"
  resource_group_name       = "${azurerm_resource_group.rg.name}"
  network_security_group_id = "${azurerm_network_security_group.nsg.id}"
  tags                      = "${var.tags}"

  ip_configuration {
    name                          = "${var.prefix}NICConfg"
    subnet_id                     = "${azurerm_subnet.subnet.id}"
    private_ip_address_allocation = "dynamic"
    public_ip_address_id          = "${azurerm_public_ip.publicip.id}"
  }
}

# Create a Linux virtual machine
resource "azurerm_virtual_machine" "vm" {
  name                  = "${var.prefix}TFVM"
  location              = "${var.location}"
  resource_group_name   = "${azurerm_resource_group.rg.name}"
  network_interface_ids = ["${azurerm_network_interface.nic.id}"]
  vm_size               = "Standard_D2S_v3"
  tags                  = "${var.tags}"

  storage_os_disk {
    name              = "${var.prefix}OsDisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Premium_LRS"
  }

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "${lookup(var.sku, var.location)}"
    version   = "latest"
  }

  os_profile {
    computer_name  = "${var.prefix}TFVM"
    admin_username = "skachar"
    admin_password = "emptyforpostonstackoverflow"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }

}

output "ip" {
  value = "${azurerm_public_ip.publicip.ip_address}"
}

output "os_sku" {
  value = "${lookup(var.sku, var.location)}"
}

下面是variables.tf文件。

variable "location" {}

variable "prefix" {
    type = "string"
    # default = "skachar"
}

variable "tags" {
    type = "map"

    default = {
        Environment = "Terraform GS"
        Dept = "Engineering"
  }
}

variable "sku" {
    default = {
        westus = "16.04-LTS"
        eastus = "18.04-LTS"
    }
}

# variable "subscription_id" {}
# variable "client_id" {}
# variable "client_secret" {}
# variable "tenant_id" {}

仅供参考-我运行的是单个VM的构建,并且构建起来没有问题。...我必须提供正确的密码,但其他所有操作都顺利进行。再次感谢任何人都可以提供的帮助。 干杯, -山姆

1 个答案:

答案 0 :(得分:0)

感谢您提出的使用count循环创建多个资源的建议,我对count很熟悉,它很有用...

但是,正如我在最初的帖子中所说的那样,我收到一条有关信用凭证的错误消息。我已经解决了这个问题。我发现通过将VS Code从PowerShell更改为PowerShell Integrated Console,可以在其中加载env变量,从而使您可以使用我发布的模块。不知道为什么第一个下拉列表不适用于多种资源。

如果有人在设置环境变量时确定在VS Code终端下拉列表中选择了第二个PowerShell环境,应该碰到这个问题。包括一个片段,以澄清我在说什么。 PowerShell Integrated Console 干杯, -山姆