如何使用 Packer + Terraform 创建小于 30GB 的自定义 Azure 映像?

How do I create custom Azure images smaller than 30GB with Packer + Terraform?

我想创建 4GB 的自定义图像,以便在业余项目中节省成本。我已经能够使用以下方法在 Terraform 中成功设置 Azure 提供的 Ubuntu 18.04 base 图像的大小:

resource "azurerm_managed_disk" "example-disk" {
  ...
  create_option = "FromImage"
  disk_size_gb = "4"
}

resource "azurerm_virtual_machine" "example" {
  ...
  vm_size = "Standard_B1s"

  storage_image_reference {
    publisher = "Canonical"
    offer = "UbuntuServer"
    sku = "18.04-LTS"
    version = "latest"
  }

  storage_os_disk {
    name = azurerm_managed_disk.example-disk.name
    managed_disk_id = azurerm_managed_disk.example-disk.id
    create_option = "Attach"
    caching = "ReadWrite"
  }
  ...
}

所以我尝试进行以下更改以使用我从这个 Ubuntu 基本图像创建的自定义 Packer 图像(根据使用托管磁盘的 terraform-provider-azurerm 文档 + 自定义图像不是很简单, 但这既不是这里也不是那里):

variable "packer_image_id" {}

variable "packer_image_name" {}

data "azurerm_image" "custom" {
  ...
  name = var.packer_image_name
}

resource "azurerm_virtual_machine" "example" {
  ...
  vm_size = "Standard_B1s"

  delete_os_disk_on_termination = true

  storage_image_reference {
    id = data.azurerm_image.custom.id
  }

  storage_os_disk {
    create_option = "FromImage"
    caching = "ReadWrite"
    disk_size_gb = "4"
  }
  ...
}

当我进行更改时出现错误:

Error: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: autorest/azure: Service returned an error. Status=<nil> Code="OperationNotAllowed" Message="The specified disk size 4 GB is smaller than the size of the corresponding disk in the VM image: 30 GB. This is not allowed. Please choose equal or greater size or do not specify an explicit size." Target="osDisk.diskSizeGB"

"No big deal",我想,"I'll just make the actual image 4GB"。因此,我尝试将行 "os_disk_size_gb": 4 添加到我的 Packer 模板中:

{
  "variables": [ ... ],
  "builders": [
    {
      "type": "azure-arm",
      "client_id": "{{ user `azure_client_id` }}",
      "client_secret": "{{ user `azure_client_secret` }}",
      "subscription_id": "{{ user `azure_subscription_id` }}",
      "tenant_id": "{{ user `azure_tenant_id` }}",
      "location": "eastus2",
      "vm_size": "Standard_B1s",
      "os_type": "Linux",
      "os_disk_size_gb": 4,
      "image_publisher": "Canonical",
      "image_offer": "UbuntuServer",
      "image_sku": "18.04-LTS",
      "ssh_username": "packer",
      "managed_image_name": "example-{{ isotime \"20060102-150405\" }}",
      "managed_image_resource_group_name": "packer-images",
      "azure_tags": {}
    }
  ],
  "provisioners": [ ... (omitting for space: just a "remote-exec" that creates a new user, downloads Tomcat, and enables service) ]
}

但是我得到这个错误:

==> azure-arm: ERROR:   -> OperationNotAllowed : The specified disk size 4 GB is smaller than the size of the corresponding disk in the VM image: 30 GB. This is not allowed. Please choose equal or greater size or do not specify an explicit size.

从 Terraform 计划中删除 disk_size_gb = "4" 并从 Packer 模板中删除 "os_disk_size_gb": 4 可以成功创建和部署映像,但我 运行 是一个 30GB 的 VM 磁盘比我需要的大。我在这里缺少什么吗?或者使用 Packer + Terraform 无法在 Azure 中拥有小于 30GB 的自定义图像?

这不是加壳限制,而是 Azure 对基本映像的限制。这些图像文件可以小至 1 GB,但默认 Ubuntu 图像有 30GB OS 磁盘。而且您不能创建磁盘小于基本映像的 VM。

https://docs.azure.cn/en-us/articles/azure-marketplace/imageguide#3-

VHD image files must be between 1GB and 1TB in size.

如果您想低于 30GB,您可能需要从头开始创建整个图像。参见例如https://docs.azure.cn/en-us/articles/azure-marketplace/imagecreateonlocal