azure-image-builder

作者: hashicorp

使用Packer构建Azure托管映像和Azure Compute Gallery映像。在创建Azure VM的自定义映像时使用。

npx skills add https://github.com/hashicorp/agent-skills --skill azure-image-builder

Azure Image Builder

Build Azure managed images and Azure Compute Gallery images using Packer's azure-arm builder.

Reference: Azure ARM Builder

Note: Building Azure images incurs costs (compute, storage, data transfer). Builds typically take 15-45 minutes depending on provisioning and OS.

Basic Managed Image

packer {
  required_plugins {
    azure = {
      source  = "github.com/hashicorp/azure"
      version = "~> 2.0"
    }
  }
}

variable "client_id" {
  type      = string
  sensitive = true
}

variable "client_secret" {
  type      = string
  sensitive = true
}

variable "subscription_id" {
  type = string
}

variable "tenant_id" {
  type = string
}

variable "resource_group" {
  type    = string
  default = "packer-images-rg"
}

locals {
  timestamp = regex_replace(timestamp(), "[- TZ:]", "")
}

source "azure-arm" "ubuntu" {
  client_id       = var.client_id
  client_secret   = var.client_secret
  subscription_id = var.subscription_id
  tenant_id       = var.tenant_id

  managed_image_resource_group_name = var.resource_group
  managed_image_name                = "my-app-${local.timestamp}"

  os_type         = "Linux"
  image_publisher = "Canonical"
  image_offer     = "0001-com-ubuntu-server-jammy"
  image_sku       = "22_04-lts-gen2"

  location = "East US"
  vm_size  = "Standard_B2s"

  azure_tags = {
    Name      = "my-app"
    BuildDate = local.timestamp
  }
}

build {
  sources = ["source.azure-arm.ubuntu"]

  provisioner "shell" {
    inline = [
      "sudo apt-get update",
      "sudo apt-get upgrade -y",
    ]
  }
}

Azure Compute Gallery

source "azure-arm" "ubuntu" {
  client_id       = var.client_id
  client_secret   = var.client_secret
  subscription_id = var.subscription_id
  tenant_id       = var.tenant_id

  os_type         = "Linux"
  image_publisher = "Canonical"
  image_offer     = "0001-com-ubuntu-server-jammy"
  image_sku       = "22_04-lts-gen2"

  location = "East US"
  vm_size  = "Standard_B2s"

  shared_image_gallery_destination {
    resource_group       = "gallery-rg"
    gallery_name         = "myImageGallery"
    image_name           = "ubuntu-webapp"
    image_version        = "1.0.${formatdate("YYYYMMDD", timestamp())}"
    replication_regions  = ["East US", "West US 2"]
    storage_account_type = "Standard_LRS"
  }
}

Authentication

Service Principal

# Create service principal
az ad sp create-for-rbac \
  --name "packer-sp" \
  --role Contributor \
  --scopes /subscriptions/<subscription-id>

# Set environment variables
export ARM_CLIENT_ID="<client-id>"
export ARM_CLIENT_SECRET="<client-secret>"
export ARM_SUBSCRIPTION_ID="<subscription-id>"
export ARM_TENANT_ID="<tenant-id>"

Managed Identity

source "azure-arm" "ubuntu" {
  use_azure_cli_auth = true
  subscription_id    = var.subscription_id
  # ... rest of configuration
}

Build Commands

# Set authentication
export ARM_CLIENT_ID="your-client-id"
export ARM_CLIENT_SECRET="your-client-secret"
export ARM_SUBSCRIPTION_ID="your-subscription-id"
export ARM_TENANT_ID="your-tenant-id"

# Initialize plugins
packer init .

# Validate template
packer validate .

# Build image
packer build .

Common Issues

Authentication Failed

  • Verify service principal credentials
  • Ensure Contributor role on resource group
  • Check subscription and tenant IDs

Compute Gallery Version Exists

  • Image versions are immutable
  • Use unique version numbers with date/build number
  • Cannot overwrite existing versions

Timeout During Provisioning

  • Check network connectivity from build VM
  • Verify NSG rules allow required traffic
  • Increase timeout if needed

References

来自 hashicorp 的更多技能

provider-actions
hashicorp
使用Plugin Framework实现Terraform Provider操作。在开发于生命周期事件(之前/之后…)执行的命令式操作时使用。
official
new-terraform-provider
hashicorp
在使用 Plugin Framework 搭建新的 Terraform provider 时使用:工作区布局、go module 设置、provider server 的 main.go 和 provider.go…
official
terraform-test
hashicorp
编写和运行Terraform测试的综合指南。在创建测试文件(.tftest.hcl)、使用run块编写测试场景、验证……时使用。
official
terraform-test
hashicorp
关于使用断言、模拟和模块验证编写及运行Terraform测试的全面指南。使用.tftest.hcl语法编写测试文件,通过运行块在计划或应用模式下执行,支持顺序和并行执行,并可选择状态隔离。对资源属性、输出和数据源进行断言条件验证;使用expect_failures确保无效输入被正确拒绝。模拟提供程序(Terraform 1.7.0+)可模拟基础设施行为,无需...
official
provider-actions
hashicorp
使用Plugin Framework在资源生命周期事件中实现命令式Terraform Provider操作。支持创建前/后和更新前/后的生命周期触发器(Terraform 1.14.0中不支持销毁事件)。需要正确的模式定义,包括框架类型、集合的ElementType以及输入验证的验证器。包含进度报告、超时管理和长时间运行操作的全面错误处理。实现轮询和...
official
aws-ami-builder
hashicorp
使用Packer的amazon-ebs构建器创建自定义Amazon Machine Images。通过HCL模板自动化从源AMI创建AMI的过程,并利用配置器(shell脚本、文件上传、配置管理)进行自定义。支持通过ami_regions实现多区域AMI分发,以及按名称、所有者和虚拟化类型灵活过滤源AMI。通过环境变量、AWS凭证文件或IAM实例配置文件进行身份验证;包含模板的验证和构建命令...
official
new-terraform-provider
hashicorp
使用Plugin Framework搭建一个新的Terraform provider。生成一个采用标准"terraform-provider-"命名约定的新Go模块工作区,并初始化所需依赖。提供一个遵循HashiCorp Plugin Framework模式的模板main.go文件,其中包含用于自定义的TODO标记。通过运行构建和测试命令来验证设置,确保provider能够编译并通过初始检查。通过创建新工作区前确认意图来处理工作区管理。
official
azure-verified-modules
hashicorp
针对寻求AVM合规的Azure Terraform模块的认证要求与最佳实践。强制要求提供者版本约束(azurerm >= 4.0, < 5.0;azapi >= 2.0, < 3.0),禁止使用基于Git的模块引用,转而采用固定的Terraform注册表源。所有标识符必须使用小写下划线命名法,变量类型需精确指定,通过防腐层模式实现离散输出属性,本地变量需按字母顺序排列。新增资源需配置功能开关变量...
official