以下是使用 Terraform 版本 1.2.3 在 Azure 上创建带有 Vnet 的虚拟机的代码,其中 Vnet 地址为 192.168.10.0/24,虚拟机 IP 为 192.168.10.100,NSG 名称为 vm-nsg,操作系统为 CentOS 8.0,资源组为 test:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "test" {
  name     = "test"
  location = "East US"
}

resource "azurerm_virtual_network" "test" {
  name                = "test-vnet"
  address_space       = ['192.168.10.0/24']
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
}

resource "azurerm_subnet" "test" {
  name                 = "test-subnet"
  address_prefixes     = ['192.168.10.0/24']
  virtual_network_name = azurerm_virtual_network.test.name
  resource_group_name  = azurerm_resource_group.test.name
}

resource "azurerm_network_security_group" "test" {
  name                = "vm-nsg"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name

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

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

  ip_configuration {
    name                          = "test-ipconfig"
    subnet_id                     = azurerm_subnet.test.id
    private_ip_address_allocation = "Static"
    private_ip_address            = "192.168.10.100"
    public_ip_address_id          = null
  }
}

resource "azurerm_virtual_machine" "test" {
  name                  = "test-vm"
  location              = azurerm_resource_group.test.location
  resource_group_name   = azurerm_resource_group.test.name
  network_interface_ids = [azurerm_network_interface.test.id]
  vm_size               = "Standard_B1s"

  storage_image_reference {
    publisher = "OpenLogic"
    offer     = "CentOS"
    sku       = "8_0"
    version   = "latest"
  }

  storage_os_disk {
    name              = "test-osdisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  os_profile {
    computer_name  = "test-vm"
    admin_username = "adminuser"
    admin_password = "P@ssw0rd1234!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }

  boot_diagnostics {
    storage_account_uri = "https://<storage_account_name>.blob.core.windows.net/"
  }
}

请注意,此代码中的存储帐户 URI 应替换为实际的存储帐户 URI。此外,您需要使用正确的凭据对 Terraform 进行身份验证,以便使用 Azure API 在 Azure 上创建这些资源。

使用 Terraform 1.2.3 在 Azure 上创建带有 VNet 的虚拟机 - 详细教程

原文地址: https://www.cveoy.top/t/topic/mJ9p 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录