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

﹥>﹥吖頭↗ 提交于 2020-04-30 10:01:17

问题


I want to create custom images that are 4GB for cost-saving purposes on a side project. I've been able set the size for the Azure-provided Ubuntu 18.04 base image in Terraform successfully using the following:

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"
  }
  ...
}

So I tried making the following changes to use a custom Packer image that I had created from this Ubuntu base image (per the terraform-provider-azurerm docs using a managed disk + custom image not very straightforward, but that's neither here not there):

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"
  }
  ...
}

When I make that change but I get the error:

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 thought, "I'll just make the actual image 4GB". So, I tried adding the line "os_disk_size_gb": 4 to my Packer template:

{
  "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) ]
}

But I get this error:

==> 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.

Removing both disk_size_gb = "4" from the Terraform plan and "os_disk_size_gb": 4 from the Packer template results in successful image creation and deploy, but I'm running a 30GB VM disk which is way larger than what I need. Is there anything I'm missing here? Or it is just not possible to have have custom images in Azure less than 30GB using Packer + Terraform?


回答1:


This is not a packer restriction but rather an restriction of Azure with regards to the base image. Those image files can be as small as 1 GB, but the default Ubuntu image has a 30GB OS disk. And you can't create a VM with a disk smaller than the base image.

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

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

You probably need to create the whole image from scratch if you want to go below 30GB. See e.g. https://docs.azure.cn/en-us/articles/azure-marketplace/imagecreateonlocal



来源:https://stackoverflow.com/questions/60699151/how-do-i-create-custom-azure-images-smaller-than-30gb-with-packer-terraform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!