Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 36 ยท Projects and Practice

Project: A Multi-cloud Image Matrix

Design one Packer build that produces matching images on AWS, Azure and Google Cloud: shared provisioning, per-cloud sources, naming rules, a combined manifest and a parallel pipeline.

Many organizations run in more than one cloud, or want to avoid lock-in. Packer's central idea, one template and many builders, fits that perfectly. In this project you design a build that produces the same logical image (call it platform-base) as an AWS AMI, an Azure Compute Gallery version and a Google Cloud image, from shared provisioning, and wire the results together in a pipeline.

None of the clouds or Packer can be reached from this site. The HCL is read-only; run it in your own accounts after replacing placeholders. The JavaScript stand-ins check naming and combine manifests.

Design goals

  • One source of truth for provisioning: the same scripts or Ansible role run on all clouds.
  • Per-cloud differences isolated in the source blocks (auth, base image, networking, naming).
  • One version number drives every cloud name (Azure and Google images).
  • One manifest per cloud merged into a single release record.
  • Failure isolation: a problem in one cloud must not hide results from the others.

Repository layout

TEXT
images/platform-base/
  platform.pkr.hcl      # packer block, shared locals
  sources.pkr.hcl       # amazon-ebs, azure-arm, googlecompute sources
  build.pkr.hcl         # build block, provisioners, post-processors
  variables.pkr.hcl
  scripts/ ...

Packer loads every *.pkr.hcl in the directory, so you can split files as you like.

Sources

HCL
locals {
  image_name  = "platform-base"
  dashed      = replace(var.version, ".", "-")
}

source "amazon-ebs" "aws" {
  region        = "eu-central-1"
  instance_type = "t3.small"
  ssh_username  = "ubuntu"
  ami_name      = "${local.image_name}-${var.version}"
  source_ami_filter {
    filters     = { name = "ubuntu/images/hvm-ssd-gp3/ubuntu-*-24.04-amd64-server-*" }
    owners      = ["099720109477"]
    most_recent = true
  }
}

source "azure-arm" "azure" {
  use_azure_cli_auth = true
  subscription_id    = var.azure_subscription_id
  image_publisher    = "Canonical"
  image_offer        = "ubuntu-24_04-lts"
  image_sku          = "server"
  location           = "westeurope"
  vm_size            = "Standard_B2s"
  os_type            = "Linux"
  shared_image_gallery_destination {
    subscription   = var.azure_subscription_id
    resource_group = "rg-images"
    gallery_name   = "corp_gallery"
    image_name     = local.image_name
    image_version  = var.version
  }
}

source "googlecompute" "gcp" {
  project_id          = var.gcp_project_id
  zone                = "europe-west3-a"
  source_image_family = "ubuntu-2404-lts-amd64"
  ssh_username        = "packer"
  image_name          = "${local.image_name}-${local.dashed}"
  image_family        = local.image_name
}

The Azure arguments for the gallery destination and the exact source image values depend on plugin version; confirm them in the plugin documentation before use.

One build, shared provisioning

HCL
build {
  sources = [
    "source.amazon-ebs.aws",
    "source.azure-arm.azure",
    "source.googlecompute.gcp",
  ]

  provisioner "shell" {
    scripts          = ["scripts/base.sh", "scripts/agents.sh", "scripts/cleanup.sh"]
    environment_vars = ["VERSION=${var.version}"]
    execute_command  = "sudo -E bash '{{ .Path }}'"
  }

  provisioner "shell" {
    only   = ["amazon-ebs.aws"]
    inline = ["sudo systemctl enable amazon-ssm-agent"]
  }

  post-processor "manifest" {
    output      = "manifest-${source.name}.json"
    custom_data = { version = var.version }
  }
}

only limits a step to one source, which is how you add cloud-specific agents. The source.name expression can differ across Packer versions; if it is unavailable in yours, produce the manifest per cloud with -only in the pipeline instead.

By default, builds for all sources run in parallel. Use -parallel-builds=1 for readable logs while debugging, and -except= to skip a cloud temporarily.

Pipeline

Run one CI job per cloud (a matrix with -only), each with its own short-lived credentials (OIDC to AWS, Azure and Google). A final job downloads all manifests and merges them into one release record, then runs a boot test per cloud and promotes all three together only if all pass. Promoting only some clouds leaves your platform inconsistent.

flowchart LR
  T["One template"] --> A["AWS job"]
  T --> Z["Azure job"]
  T --> G["GCP job"]
  A --> M["merge manifests"]
  Z --> M
  G --> M
  M --> X["boot tests per cloud"] --> P["promote together"]

Three parallel builds, one release decision.

Try it: merge manifests

JS Combine per-cloud manifests (stand-in)

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

No output.

Try it: promote only if all pass

JS All-or-nothing promotion (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. How do you run a provisioner on only one of several sources?

    only and except select sources by their type.name address.
  2. 2. Why keep cloud differences inside source blocks?

    That separation keeps one template maintainable across clouds.
  3. 3. Why promote all clouds together?

    Partial promotion makes behavior differ between clouds.
  4. 4. How can a pipeline keep one failing cloud from hiding the others' results?

    Independent jobs report each cloud separately.

Summary

  • One template with several sources shares provisioning across clouds.
  • Isolate per-cloud settings in sources; use only for cloud-specific steps.
  • Derive every cloud's name from one version and merge manifests into a single release record.
  • Promote all clouds together after per-cloud boot tests.

Next: Project: a hardened base image pipeline