【问题标题】:create azure vm from image using terraform使用 terraform 从图像创建 azure vm
【发布时间】:2019-07-14 03:21:47
【问题描述】:

我参考了github代码。请在下面找到URL

https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples/vm-from-managed-image

我修改了脚本并执行了 terraform init。我收到以下错误。

读取 azurerm_network_interface[main] 的配置时出错:在 1:18 解析错误:预期为“)”但找到了“.”[0m

我的脚本:

    # Configure the Microsoft Azure Provider
provider "azurerm" {
    subscription_id = "xxxxxxxx"
    client_id       = "xxxxxxxx"
    client_secret   = "xxxxxxxx"
    tenant_id       = "xxxxxxxx"
}

# Locate the existing custom/golden image
data "azurerm_image" "search" {
  name                = "AZLXSPTDEVOPS01_Image"
  resource_group_name = "RG-PLATFORM"
}

output "image_id" {
  value = "/subscriptions/4f5c9f2a-3584-4bbd-a26e-bbf69ffbfbe6/resourceGroups/RG-EASTUS-SPT-PLATFORM/providers/Microsoft.Compute/images/AZLXSPTDEVOPS01_Image"
}

# Create a Resource Group for the new Virtual Machine.
resource "azurerm_resource_group" "main" {
  name     = "RG-TEST"
  location = "eastus"
}

# Create a Virtual Network within the Resource Group
resource "azurerm_virtual_network" "main" {
  name                = "RG-Vnet"
  address_space       = ["10.100.0.0/16"]
  resource_group_name = "${azurerm_resource_group.main.name}"
  location            = "${azurerm_resource_group.main.location}"
}

# Create a Subnet within the Virtual Network
resource "azurerm_subnet" "internal" {
  name                 = "RG-Terraform-snet-in"
  virtual_network_name = "${azurerm_virtual_network.main.name}"
  resource_group_name  = "${azurerm_resource_group.main.name}"
  address_prefix       = "10.100.2.0/24"
}

# Create a Network Security Group with some rules
resource "azurerm_network_security_group" "main" {
  name                = "RG-QA-Test-Web-NSG"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"

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

# Create a network interface for VMs and attach the PIP and the NSG
resource "azurerm_network_interface" "main" {
  name                      = "myNIC"
  location                  = "${azurerm_resource_group.main.location}"
  resource_group_name       = "${azurerm_resource_group.main.name}"
  network_security_group_id = "${azurerm_network_security_group.main.id}"

  ip_configuration {
    name                          = "primary"
    subnet_id                     = "${azurerm_subnet.internal.id}"
    private_ip_address_allocation = "static"
    private_ip_address            = "${cidrhost("10.100.1.8/24", 4)}"
  }
}

# Create a new Virtual Machine based on the Golden Image
resource "azurerm_virtual_machine" "vm" {
  name                             = "AZLXSPTDEVOPS01"
  location                         = "${azurerm_resource_group.main.location}"
  resource_group_name              = "${azurerm_resource_group.main.name}"
  network_interface_ids            = ["${azurerm_network_interface.main.id}"]
  vm_size                          = "Standard_DS12_v2"
  delete_os_disk_on_termination    = true
  delete_data_disks_on_termination = true

  storage_image_reference {
    id = "${data.azurerm_image.search.id}"
  }

  storage_os_disk {
    name              = "AZLXSPTDEVOPS01-OS"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
}

  os_profile {
    computer_name  = "APPVM"
    admin_username = "admin"
    admin_password = "admin#2019"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}

【问题讨论】:

  • 这对我来说看起来像是一个简单的错字。您需要在"${cidrhost(10.100.1.8/24, 4)}" 的 IP 范围内加上引号。所以应该是"${cidrhost("10.100.1.8/24", 4)}"。
  • 谢谢。但我收到两个新错误。 1. azurerm_subnet.internal:发生 1 个错误:* azurerm_subnet.internal:创建/更新子网“RG”(虚拟网络“RG-Vnet”/资源组“RG”)时出错:network.SubnetsClient#CreateOrUpdate:发送失败请求:StatusCode=400 -- 原始错误:代码="NetcfgInvalidSubnet" 消息="子网 'RG-out' 在虚拟网络 'RG-Vnet' 中无效。"详细信息=[]
  • 2. azurerm_virtual_network.main:发生 1 个错误:* azurerm_virtual_network.main:创建/更新虚拟网络“RG-Vnet”(资源组“RG-TEST”)时出错:network.VirtualNetworksClient#CreateOrUpdate:发送请求失败:StatusCode=400 -- 原始错误:Code="InUseSubnetCannotBeDeleted" Message="Subnet RG-Terraform-snet-out 正在被 /subscriptions/4f5c9f2a-3584-4bbd-a26e-bbf69ffbfbe6/resourceGroups/RG-OPT-QA-TEST/providers/ 使用Microsoft.Network/networkInterfaces/myNIC/ipConfigurations/myNicConfiguration 并且无法删除。”详细信息=[]
  • 虽然我的回答可以帮助你解决问题,但为什么不接受呢?
  • 当我尝试在新防火墙中创建 vm 时遇到错误。 azurerm_subnet.internal:创建/更新子网“VNET-PFSENSE-TEST/SNET-IN”时出错(虚拟网络“VNET-PFSENSE-TEST”/资源组“RG-PF-TEST”):network.SubnetsClient#CreateOrUpdate:发送失败请求:StatusCode=404 -- 原始错误:Code="Failed" Message="异步操作失败。" AdditionalInfo=[{"Message":"未找到与请求 URI 匹配的 HTTP 资源

标签: image azure virtual-machine terraform orchestration


【解决方案1】:

以下脚本运行良好

# Configure the Microsoft Azure Provider
provider "azurerm" {
    subscription_id = "xxxx"
    client_id       = "xxxx"
    client_secret   = "xxxx"
    tenant_id       = "xxxx"
}

# Locate the existing custom/golden image
data "azurerm_image" "search" {
  name                = "AZDEVOPS01_Image"
  resource_group_name = "RG-PLATFORM"
}

output "image_id" {
  value = "/subscriptions/xxxxxx/resourceGroups/RG-EASTUS-SPT-PLATFORM/providers/Microsoft.Compute/images/AZLXDEVOPS01_Image"
}

# Create a Resource Group for the new Virtual Machine.
resource "azurerm_resource_group" "main" {
  name     = "RG-OPT-QA-TEST"
  location = "eastus"
}

# Create a Subnet within the Virtual Network
resource "azurerm_subnet" "internal" {
  name                 = "RG-Terraform-snet-in"
  virtual_network_name = "RG-OPT-QA-Vnet"
  resource_group_name  = "${azurerm_resource_group.main.name}"
  address_prefix       = "10.100.2.0/24"
}

# Create a Network Security Group with some rules
resource "azurerm_network_security_group" "main" {
  name                = "RG-QA-Test-Dev-NSG"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"

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

# Create a network interface for VMs and attach the PIP and the NSG
resource "azurerm_network_interface" "main" {
  name                      = "NIC"
  location                  = "${azurerm_resource_group.main.location}"
  resource_group_name       = "${azurerm_resource_group.main.name}"
  network_security_group_id = "${azurerm_network_security_group.main.id}"

  ip_configuration {
    name                          = "nicconfig"
    subnet_id                     = "${azurerm_subnet.internal.id}"
    private_ip_address_allocation = "static"
    private_ip_address            = "${cidrhost("10.100.2.16/24", 4)}"
  }
}

# Create a new Virtual Machine based on the Golden Image
resource "azurerm_virtual_machine" "vm" {
  name                             = "AZLXDEVOPS01"
  location                         = "${azurerm_resource_group.main.location}"
  resource_group_name              = "${azurerm_resource_group.main.name}"
  network_interface_ids            = ["${azurerm_network_interface.main.id}"]
  vm_size                          = "Standard_DS12_v2"
  delete_os_disk_on_termination    = true
  delete_data_disks_on_termination = true

  storage_image_reference {
    id = "${data.azurerm_image.search.id}"
  }

  storage_os_disk {
    name              = "AZLXDEVOPS01-OS"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
}

  os_profile {
    computer_name  = "APPVM"
    admin_username = "devopsadmin"
    admin_password = "Cssladmin#2019"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}

【讨论】:

    【解决方案2】:

    好吧,鉴于您评论中的错误,我认为您应该像这样设置子网:

    resource "azurerm_subnet" "internal" {
      name                 = "RG-Terraform-snet-in"
      virtual_network_name = "${azurerm_virtual_network.main.name}"
      resource_group_name  = "${azurerm_resource_group.main.name}"
      address_prefix       = "10.100.1.0/24"
    }
    

    还有虚拟网络的错误,我没有在代码中看到名为“RG-Vnet”的虚拟网络,如错误所述。因此,您应该检查一下您的代码中是否一切正常。

    要从 Azure 市场中的映像创建 Azure VM,您可以按照教程 Create a complete Linux virtual machine infrastructure in Azure with Terraform 进行操作。您无需在 Terraform 代码中创建图像资源。只需在资源azurerm_virtual_machine中这样设置即可:

    storage_os_disk {
            name              = "myOsDisk"
            caching           = "ReadWrite"
            create_option     = "FromImage"
            managed_disk_type = "Premium_LRS"
        } 
    

    此外,当您在同一代码中引用其他资源时,您应该这样做:

    virtual_network_name = "${azurerm_virtual_network.main.name}"
    

    不仅仅是字符串名称为“RG-Vnet”,这不是正确的方法。

    【讨论】:

    • 感谢您的回复。现在解决了一个错误。我又收到一个错误。
    • @MohanKanth 还有问题吗?或者,如果它有帮助,您可以接受它作为答案。
    • 发生 1 个错误:* azurerm_virtual_network.main:发生 1 个错误:* azurerm_virtual_network.main:创建/更新虚拟网络“RG-Vnet”(资源组“RG”)时出错: network.VirtualNetworksClient#CreateOrUpdate: 发送请求失败: StatusCode=400 -- 原始错误: Code="InUseSubnetCannotBeDeleted" Message="Subnet RG-Terraform-snet-out is in use by /subscriptions/xxxxxxxx/resourceGroups/RG-OPT- QA-TEST/providers/Microsoft.Network/networkInterfaces/myNIC/ipConfigurations/myNicConfiguration 并且无法删除。”详细信息=[]
    • @MohanKanth 正如我在答案中所说,我在您的代码中找不到名为 RG-Vnet 的虚拟网络。你显示所有代码吗?
    • @MohanKanth 另外,您的虚拟网络没有问题,如果您按照我的建议更改子网的设置,我可以看到您的代码。
    猜你喜欢
    • 2022-10-02
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 2019-03-14
    • 2020-07-04
    • 2020-07-26
    相关资源
    最近更新 更多