$npx -y skills add hashicorp/agent-skills --skill azure-image-builderBuild Azure managed images and Azure Compute Gallery images with Packer. Use when creating custom images for Azure VMs.
| 1 | # Azure Image Builder |
| 2 | |
| 3 | Build Azure managed images and Azure Compute Gallery images using Packer's `azure-arm` builder. |
| 4 | |
| 5 | **Reference:** [Azure ARM Builder](https://developer.hashicorp.com/packer/integrations/hashicorp/azure/latest/components/builder/arm) |
| 6 | |
| 7 | > **Note:** Building Azure images incurs costs (compute, storage, data transfer). Builds typically take 15-45 minutes depending on provisioning and OS. |
| 8 | |
| 9 | ## Basic Managed Image |
| 10 | |
| 11 | ```hcl |
| 12 | packer { |
| 13 | required_plugins { |
| 14 | azure = { |
| 15 | source = "github.com/hashicorp/azure" |
| 16 | version = "~> 2.0" |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | variable "client_id" { |
| 22 | type = string |
| 23 | sensitive = true |
| 24 | } |
| 25 | |
| 26 | variable "client_secret" { |
| 27 | type = string |
| 28 | sensitive = true |
| 29 | } |
| 30 | |
| 31 | variable "subscription_id" { |
| 32 | type = string |
| 33 | } |
| 34 | |
| 35 | variable "tenant_id" { |
| 36 | type = string |
| 37 | } |
| 38 | |
| 39 | variable "resource_group" { |
| 40 | type = string |
| 41 | default = "packer-images-rg" |
| 42 | } |
| 43 | |
| 44 | locals { |
| 45 | timestamp = regex_replace(timestamp(), "[- TZ:]", "") |
| 46 | } |
| 47 | |
| 48 | source "azure-arm" "ubuntu" { |
| 49 | client_id = var.client_id |
| 50 | client_secret = var.client_secret |
| 51 | subscription_id = var.subscription_id |
| 52 | tenant_id = var.tenant_id |
| 53 | |
| 54 | managed_image_resource_group_name = var.resource_group |
| 55 | managed_image_name = "my-app-${local.timestamp}" |
| 56 | |
| 57 | os_type = "Linux" |
| 58 | image_publisher = "Canonical" |
| 59 | image_offer = "0001-com-ubuntu-server-jammy" |
| 60 | image_sku = "22_04-lts-gen2" |
| 61 | |
| 62 | location = "East US" |
| 63 | vm_size = "Standard_B2s" |
| 64 | |
| 65 | azure_tags = { |
| 66 | Name = "my-app" |
| 67 | BuildDate = local.timestamp |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | build { |
| 72 | sources = ["source.azure-arm.ubuntu"] |
| 73 | |
| 74 | provisioner "shell" { |
| 75 | inline = [ |
| 76 | "sudo apt-get update", |
| 77 | "sudo apt-get upgrade -y", |
| 78 | ] |
| 79 | } |
| 80 | } |
| 81 | ``` |
| 82 | |
| 83 | ## Azure Compute Gallery |
| 84 | |
| 85 | ```hcl |
| 86 | source "azure-arm" "ubuntu" { |
| 87 | client_id = var.client_id |
| 88 | client_secret = var.client_secret |
| 89 | subscription_id = var.subscription_id |
| 90 | tenant_id = var.tenant_id |
| 91 | |
| 92 | os_type = "Linux" |
| 93 | image_publisher = "Canonical" |
| 94 | image_offer = "0001-com-ubuntu-server-jammy" |
| 95 | image_sku = "22_04-lts-gen2" |
| 96 | |
| 97 | location = "East US" |
| 98 | vm_size = "Standard_B2s" |
| 99 | |
| 100 | shared_image_gallery_destination { |
| 101 | resource_group = "gallery-rg" |
| 102 | gallery_name = "myImageGallery" |
| 103 | image_name = "ubuntu-webapp" |
| 104 | image_version = "1.0.${formatdate("YYYYMMDD", timestamp())}" |
| 105 | replication_regions = ["East US", "West US 2"] |
| 106 | storage_account_type = "Standard_LRS" |
| 107 | } |
| 108 | } |
| 109 | ``` |
| 110 | |
| 111 | ## Authentication |
| 112 | |
| 113 | ### Service Principal |
| 114 | ```bash |
| 115 | # Create service principal |
| 116 | az ad sp create-for-rbac \ |
| 117 | --name "packer-sp" \ |
| 118 | --role Contributor \ |
| 119 | --scopes /subscriptions/<subscription-id> |
| 120 | |
| 121 | # Set environment variables |
| 122 | export ARM_CLIENT_ID="<client-id>" |
| 123 | export ARM_CLIENT_SECRET="<client-secret>" |
| 124 | export ARM_SUBSCRIPTION_ID="<subscription-id>" |
| 125 | export ARM_TENANT_ID="<tenant-id>" |
| 126 | ``` |
| 127 | |
| 128 | ### Managed Identity |
| 129 | ```hcl |
| 130 | source "azure-arm" "ubuntu" { |
| 131 | use_azure_cli_auth = true |
| 132 | subscription_id = var.subscription_id |
| 133 | # ... rest of configuration |
| 134 | } |
| 135 | ``` |
| 136 | |
| 137 | ## Build Commands |
| 138 | |
| 139 | ```bash |
| 140 | # Set authentication |
| 141 | export ARM_CLIENT_ID="your-client-id" |
| 142 | export ARM_CLIENT_SECRET="your-client-secret" |
| 143 | export ARM_SUBSCRIPTION_ID="your-subscription-id" |
| 144 | export ARM_TENANT_ID="your-tenant-id" |
| 145 | |
| 146 | # Initialize plugins |
| 147 | packer init . |
| 148 | |
| 149 | # Validate template |
| 150 | packer validate . |
| 151 | |
| 152 | # Build image |
| 153 | packer build . |
| 154 | ``` |
| 155 | |
| 156 | ## Common Issues |
| 157 | |
| 158 | **Authentication Failed** |
| 159 | - Verify service principal credentials |
| 160 | - Ensure Contributor role on resource group |
| 161 | - Check subscription and tenant IDs |
| 162 | |
| 163 | **Compute Gallery Version Exists** |
| 164 | - Image versions are immutable |
| 165 | - Use unique version numbers with date/build number |
| 166 | - Cannot overwrite existing versions |
| 167 | |
| 168 | **Timeout During Provisioning** |
| 169 | - Check network connectivity from build VM |
| 170 | - Verify NSG rules allow required traffic |
| 171 | - Increase timeout if needed |
| 172 | |
| 173 | ## References |
| 174 | |
| 175 | - [Azure ARM Builder](https://developer.hashicorp.com/packer/integrations/hashicorp/azure/latest/components/builder/arm) |
| 176 | - [Azure Compute Gallery](https://learn.microsoft.com/en-us/azure/virtual-machines/azure-compute-gallery) |