Terraform:获取 Azure 中 Windows 虚拟机的公共 IP

Terraform:获取 Azure 中 Windows 虚拟机的公共 IP

我一直在尝试弄清楚如何获取 Azure 中新配置的 VM 的公共 IP。

我使用以下示例: https://www.terraform.io/docs/providers/azurerm/d/public_ip.html

但是它不起作用:

provider "azurerm" {
  version = "= 2.0.0"
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "test-resources"
  location = "West US 2"
}

resource "azurerm_virtual_network" "example" {
  name                = "test-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "acctsub"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefix     = "10.0.2.0/24"
}

resource "azurerm_public_ip" "example" {
  name                    = "test-pip"
  location                = azurerm_resource_group.example.location
  resource_group_name     = azurerm_resource_group.example.name
  allocation_method       = "Dynamic"
  idle_timeout_in_minutes = 30

  tags = {
    environment = "test"
  }
}

resource "azurerm_network_interface" "example" {
  name                = "test-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Static"
    private_ip_address            = "10.0.2.5"
    public_ip_address_id          = azurerm_public_ip.example.id
  }
}

resource "azurerm_windows_virtual_machine" "example" {
  name                  = "myvm1"
  location              = azurerm_resource_group.example.location
  resource_group_name   = azurerm_resource_group.example.name
  network_interface_ids = [azurerm_network_interface.example.id]
  size                  = "Standard_B1s"
  admin_username        = "adminuser"
  admin_password        = "Password123!"

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2019-Datacenter"
    version   = "latest"
  }

  os_disk {
    caching           = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }
}

data "azurerm_public_ip" "example" {
  name                = azurerm_public_ip.example.name
  resource_group_name = azurerm_windows_virtual_machine.example.resource_group_name
}

output "public_ip_address" {
  value = data.azurerm_public_ip.example.ip_address
}

产量:

------------------------------------------------------------------------

Error: Error: Public IP "test-pip" (Resource Group "test-resources") was not found

  on Test.tf line 73, in data "azurerm_public_ip" "example":
  73: data "azurerm_public_ip" "example" {

答案1

这确实很奇怪,但是 terraform 上的示例不起作用,但是阅读:https://learn.hashicorp.com/tutorials/terraform/azure-dependency?in=terraform/azure-get-started

我可以看到在这个例子中他们添加了如下依赖项:

data "azurerm_public_ip" "example" {
  name                = azurerm_public_ip.example.name
  resource_group_name = azurerm_windows_virtual_machine.example.resource_group_name
  depends_on          = [azurerm_windows_virtual_machine.example]
}

这样做可以解决问题并且示例代码现在可以正常工作......

相关内容