如何在Terraform中使用来自Azure Marketplace的图像

6rqinv9w  于 2022-12-24  发布在  其他
关注(0)|答案(1)|浏览(100)

我正在尝试使用Terraform在Azure中创建虚拟机。我可以直接在Azure Marketplace中创建虚拟机。但是,我对使用Terraform创建虚拟机很感兴趣。
这是一台Linux VM计算机,我知道块source_image_reference属于资源azurerm_linux_virtual_machine
此块如下所示:

source_image_reference {
  publisher = "Canonical"
  offer     = "UbuntuServer"
  sku       = "16.04-LTS"
  version   = "latest"
}

我应该如何使用Azure Marketplace中的映像来完成此类阻止?
干杯

编辑1

上面显示的source_image_reference只是一个例子,实际上,这个例子可以在Terraform网站上找到,我想使用的图像是SQL Server 2019 on Ubuntu Server 20.04 LTS
然而,我想说这个问题更为普遍。我的意思是,一旦我在Azure Marketplace中找到了一张图片,我该如何在Terraform计划中使用它?

ckocjqey

ckocjqey1#

我在自己的环境中尝试,成功创建了Linux虚拟机,映像为***Ubuntu Server 20.04 LTS上的SQL Server 2019***

地形代码:

provider "azurerm" {
  features {}

}
resource "azurerm_resource_group" "rg" {
  name     = "example-resources"
  location = "eastus"
}

resource "azurerm_virtual_network" "my_terraform_network" {
  name                = "myVnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
}

# Create subnet
resource "azurerm_subnet" "my_terraform_subnet" {
  name                 = "mySubnet"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.my_terraform_network.name
  address_prefixes     = ["10.0.1.0/24"]
}

# Create public IPs
resource "azurerm_public_ip" "my_terraform_public_ip" {
  name                = "myPublicIP"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  allocation_method   = "Dynamic"
}

# Create Network Security Group and rule
resource "azurerm_network_security_group" "my_terraform_nsg" {
  name                = "myNetworkSecurityGroup"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  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" "my_terraform_nic" {
  name                = "myNIC"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "my_nic_configuration"
    subnet_id                     = azurerm_subnet.my_terraform_subnet.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.my_terraform_public_ip.id
  }
}

# Connect the security group to the network interface
resource "azurerm_network_interface_security_group_association" "example" {
  network_interface_id      = azurerm_network_interface.my_terraform_nic.id
  network_security_group_id = azurerm_network_security_group.my_terraform_nsg.id
}

# Generate random text for a unique storage account name
resource "random_id" "random_id" {
  keepers = {
    # Generate a new ID only when a new resource group is defined
    resource_group = azurerm_resource_group.rg.name
  }

  byte_length = 8
}

# Create storage account for boot diagnostics
resource "azurerm_storage_account" "my_storage_account" {
  name                     = "diag${random_id.random_id.hex}"
  location                 = azurerm_resource_group.rg.location
  resource_group_name      = azurerm_resource_group.rg.name
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

# Create (and display) an SSH key
resource "tls_private_key" "example_ssh" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

# Create virtual machine
resource "azurerm_linux_virtual_machine" "my_terraform_vm" {
  name                  = "myVM"
  location              = azurerm_resource_group.rg.location
  resource_group_name   = azurerm_resource_group.rg.name
  network_interface_ids = [azurerm_network_interface.my_terraform_nic.id]
  size                  = "Standard_DS1_v2"

  os_disk {
    name                 = "myOsDisk"
    caching              = "ReadWrite"
    storage_account_type = "Premium_LRS"
  }

  source_image_reference {
    publisher = "MicrosoftSQLServer"
    offer     = "sql2019-ubuntu2004"
    sku       = "web"
    version   = "15.0.221108"
  }

  computer_name                   = "myvm"
  admin_username                  = "azureuser"
  disable_password_authentication = true

  admin_ssh_key {
    username   = "azureuser"
    public_key = tls_private_key.example_ssh.public_key_openssh
  }

  boot_diagnostics {
    storage_account_uri = azurerm_storage_account.my_storage_account.primary_blob_endpoint
  }
}

**SQL Server 2019 on Ubuntu Server 20.04 LTS**的源图像引用

source_image_reference {
    publisher = "MicrosoftSQLServer"
    offer     = "sql2019-ubuntu2004"
    sku       = "web"
    version   = "15.0.221108"
  }

控制台:

门户网站:

您可以通过执行以下azure cli命令来获取**versionsku**:

az vm image list --all --publisher="MicrosoftSQLServer" --offer="sql2019-ubuntu2004"

参考资料:Creating an Azure Linux VM with Ubuntu 20.04 with Terraform - Stack Overflow,由特奥多里科·马齐维亚拉提供

相关问题