以下是使用 Terraform 创建 Azure 虚拟机的代码示例:

provider "azurerm" {
  version = "=1.44.0"
}

resource "azurerm_resource_group" "test" {
  name     = "test"
  location = "eastus"
}

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"
  resource_group_name  = azurerm_resource_group.test.name
  virtual_network_name = azurerm_virtual_network.test.name
  address_prefix       = "192.168.10.0/24"
}

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                       = "allow_ssh"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

resource "azurerm_public_ip" "test" {
  name                = "test-pip"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name

  allocation_method = "Static"
}

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          = azurerm_public_ip.test.id
  }
}

resource "azurerm_linux_virtual_machine" "test" {
  name                = "test-vm"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
  size                = "Standard_B1s"
  admin_username      = "adminuser"
  disable_password_authentication = true
  network_interface_ids = [azurerm_network_interface.test.id]
  os_disk {
    name              = "test-os-disk"
    caching           = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }
  source_image_reference {
    publisher = "OpenLogic"
    offer     = "CentOS"
    sku       = "8_0"
    version   = "latest"
  }
  os_profile {
    computer_name  = "test-vm"
    admin_username = "adminuser"
    admin_ssh_key {
      username = "adminuser"
      public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8S8eH5I5j5ZJY5Y8p4Wx4v7Gd5yP4zJZ8W5F5Q5ZSJYgZJY5p4Wx4v7Gd5yP4zJZ8W5F5Q5ZSJYgZJY5p4Wx4v7Gd5yP4zJZ8W5F5Q5ZSJYgZJY5p4Wx4v7Gd5yP4zJZ8W5F5Q5ZSJYgZJY5p4Wx4v7Gd5yP4zJZ8W5F5Q5ZSadminuser@localhost"
    }
  }
}

请根据实际情况修改虚拟机的大小、用户名、公钥等参数。

使用 Terraform 在 Azure 上创建带 VNet 的 CentOS 8.0 虚拟机

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

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