Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 36 ยท Images for Cloud and VMs

Azure and Google Cloud Images with Packer

Build Azure managed images and Compute Gallery versions and Google Compute Engine images with Packer: authentication, source images, naming rules and a runnable name-mapping stand-in.

AWS is not the only target. Packer has plugins for Microsoft Azure and Google Cloud that follow the same idea as amazon-ebs: start a temporary VM from a marketplace or public image, provision it, capture the disk as a reusable image and delete the temporary resources. The vocabulary differs, so this lesson maps the concepts side by side.

Packer and the cloud APIs cannot run on this site. The HCL is read-only; the JavaScript examples are labelled stand-ins that check names and map settings. No real build output is shown.

Azure: the azure-arm builder

The azure-arm builder (from the azure plugin) uses Azure Resource Manager. It creates a temporary resource group, a VM, a network and a disk, captures the result and removes the temporary group.

HCL
packer {
  required_plugins {
    azure = {
      source  = "github.com/hashicorp/azure"
      version = ">= 2.0.0"
    }
  }
}

source "azure-arm" "ubuntu" {
  use_azure_cli_auth = true
  subscription_id    = var.subscription_id

  image_publisher = "Canonical"
  image_offer     = "ubuntu-24_04-lts"
  image_sku       = "server"
  os_type         = "Linux"
  location        = "westeurope"
  vm_size         = "Standard_B2s"

  managed_image_name                = "web-base-${var.version}"
  managed_image_resource_group_name = "rg-images"

  shared_image_gallery_destination {
    subscription        = var.subscription_id
    resource_group      = "rg-images"
    gallery_name        = "corp_gallery"
    image_name          = "web-base"
    image_version       = var.version
    replication_regions = ["westeurope", "northeurope"]
  }
}

Key points:

  • Authentication. Use the Azure CLI login (use_azure_cli_auth), a managed identity when running inside Azure, or a service principal whose secret comes from an environment variable.
  • Source image. Publisher, offer, SKU and optionally a version identify a marketplace image. az vm image list shows the values.
  • Outputs. A managed image is a single-region resource. A Compute Gallery (formerly Shared Image Gallery) image version supports versioning and replication to many regions and is the better choice for teams.
  • Gallery versions must be major.minor.patch numbers such as 1.4.0. A timestamp like 20250101 is not valid.
  • Windows. Set os_type = "Windows", communicator = "winrm" and finish with a Sysprep step; see the Windows note in the VM lesson.

Google Cloud: the googlecompute builder

The googlecompute builder creates a temporary Compute Engine instance in a project, provisions it and saves the boot disk as a custom image.

HCL
packer {
  required_plugins {
    googlecompute = {
      source  = "github.com/hashicorp/googlecompute"
      version = ">= 1.1.0"
    }
  }
}

source "googlecompute" "debian" {
  project_id          = var.project_id
  zone                = "europe-west3-a"
  source_image_family = "debian-12"
  ssh_username        = "packer"
  machine_type        = "e2-medium"

  image_name        = "web-base-${var.version}"
  image_family      = "web-base"
  image_description = "Web base image built by Packer"
  image_labels = {
    built_by = "packer"
  }
}
  • Authentication. Application Default Credentials (gcloud auth application-default login), a service account attached to the runner, or workload identity federation in CI. Avoid long-lived JSON keys.
  • Source. source_image_family picks the newest non-deprecated image of that family; source_image pins a specific one.
  • Image family. image_family lets instance templates say "latest web-base" without knowing a version. Because images are immutable, publishing a new image in the family is how you roll forward.
  • Naming rules. GCE image names must start with a lowercase letter, contain only lowercase letters, digits and hyphens, end with a letter or digit and be at most 63 characters.
  • Sharing. Grant roles/compute.imageUser on the image or project, or use image_storage_locations for multi-region storage.

Side by side

Concept AWS Azure Google Cloud
Builder amazon-ebs azure-arm googlecompute
Result AMI Managed image or gallery version Custom image
Grouping tags / name Gallery image definition Image family
Regions ami_regions gallery replication_regions image_storage_locations
Auth credential chain, roles CLI, managed identity, SP ADC, service account, WIF
flowchart LR
  T["Packer template"] --> A["amazon-ebs"]
  T --> Z["azure-arm"]
  T --> G["googlecompute"]
  A --> AMI["AMI"]
  Z --> GAL["Gallery version"]
  G --> IMG["Custom image + family"]

One build block can list several sources, so a single packer build can produce an image per cloud.

Try it: validate cloud-specific names

JS Image name validators (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Try it: one version, three naming styles

A pipeline often has one semantic version and must derive each cloud's name from it. This stand-in does the mapping.

JS Derive names from one version (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which Azure output supports versions and replication to several regions?

    Managed images live in one region; gallery versions are versioned and replicate.
  2. 2. What does image_family do on Google Cloud?

    A family always points to the latest non-deprecated image, so new builds roll forward automatically.
  3. 3. Which version string is valid for an Azure gallery image version?

    Gallery versions use the major.minor.patch format.
  4. 4. What is the safest way to authenticate Packer to Google Cloud in CI?

    Short-lived federated credentials avoid storing long-lived secrets.

Summary

  • azure-arm and googlecompute follow the same temporary-VM pattern as amazon-ebs.
  • Prefer Azure Compute Gallery versions and GCE image families for versioning.
  • Each cloud has its own naming rules; derive them from one version in the pipeline.
  • Authenticate with short-lived identities, never keys in templates.

Next: VirtualBox, VMware and QEMU images