windows-builder

द्वारा hashicorp

Build Windows images with Packer using WinRM communicator and PowerShell provisioners. Use when creating Windows AMIs, Azure images, or VMware templates.

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

Windows Builder

Platform-agnostic patterns for building Windows images with Packer.

Reference: WinRM Communicator

Note: Windows builds incur significant costs and time. Expect 45-120 minutes per build due to Windows Updates. Failed builds may leave resources running - always verify cleanup.

WinRM Communicator Setup

Windows requires WinRM for Packer communication.

AWS Example

source "amazon-ebs" "windows" {
  region        = "us-west-2"
  instance_type = "t3.medium"

  source_ami_filter {
    filters = {
      name = "Windows_Server-2022-English-Full-Base-*"
    }
    most_recent = true
    owners      = ["amazon"]
  }

  ami_name = "windows-server-2022-${local.timestamp}"

  communicator   = "winrm"
  winrm_username = "Administrator"
  winrm_use_ssl  = true
  winrm_insecure = true
  winrm_timeout  = "15m"

  user_data_file = "scripts/setup-winrm.ps1"
}

WinRM Setup Script (scripts/setup-winrm.ps1)

<powershell>
# Configure WinRM
winrm quickconfig -q
winrm set winrm/config '@{MaxTimeoutms="1800000"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'

# Configure firewall
netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow
netsh advfirewall firewall add rule name="WinRM 5986" protocol=TCP dir=in localport=5986 action=allow

# Restart WinRM
net stop winrm
net start winrm
</powershell>

Azure Example

source "azure-arm" "windows" {
  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 = "images-rg"
  managed_image_name                = "windows-${local.timestamp}"

  os_type         = "Windows"
  image_publisher = "MicrosoftWindowsServer"
  image_offer     = "WindowsServer"
  image_sku       = "2022-datacenter-g2"

  location = "East US"
  vm_size  = "Standard_D2s_v3"

  # Azure auto-configures WinRM
  communicator   = "winrm"
  winrm_use_ssl  = true
  winrm_insecure = true
  winrm_timeout  = "15m"
  winrm_username = "packer"
}

PowerShell Provisioners

Install Software

build {
  sources = ["source.amazon-ebs.windows"]

  # Install Chocolatey
  provisioner "powershell" {
    inline = [
      "Set-ExecutionPolicy Bypass -Scope Process -Force",
      "iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))"
    ]
  }

  # Install applications
  provisioner "powershell" {
    inline = [
      "choco install -y googlechrome",
      "choco install -y 7zip",
    ]
  }

  # Install IIS
  provisioner "powershell" {
    inline = [
      "Install-WindowsFeature -Name Web-Server -IncludeManagementTools"
    ]
  }
}

Windows Updates

provisioner "powershell" {
  inline = [
    "Install-PackageProvider -Name NuGet -Force",
    "Install-Module -Name PSWindowsUpdate -Force",
    "Import-Module PSWindowsUpdate",
    "Get-WindowsUpdate -Install -AcceptAll -AutoReboot",
  ]
  timeout = "2h"
}

# Wait for reboots
provisioner "windows-restart" {
  restart_timeout = "30m"
}

Cleanup

provisioner "powershell" {
  inline = [
    "# Clear temp files",
    "Remove-Item -Path 'C:\\Windows\\Temp\\*' -Recurse -Force -ErrorAction SilentlyContinue",
    "# Clear Windows Update cache",
    "Stop-Service -Name wuauserv -Force",
    "Remove-Item -Path 'C:\\Windows\\SoftwareDistribution\\*' -Recurse -Force -ErrorAction SilentlyContinue",
    "Start-Service -Name wuauserv",
  ]
}

Common Issues

WinRM Timeout

  • Increase winrm_timeout to 15m or more
  • Verify security group allows ports 5985/5986
  • Check user data script completed successfully

PowerShell Execution Policy

provisioner "powershell" {
  inline = [
    "Set-ExecutionPolicy Bypass -Scope Process -Force",
    "# Your commands here",
  ]
}

Long Build Times

  • Windows Updates can take 1-2 hours
  • Use pre-patched base images when available
  • Set provisioner timeout = "2h"

References

hashicorp की और Skills

provider-actions
hashicorp
Implement Terraform Provider actions using the Plugin Framework. Use when developing imperative operations that execute at lifecycle events (before/after…
official
new-terraform-provider
hashicorp
Use this when scaffolding a new Terraform provider with the Plugin Framework: workspace layout, go module setup, provider server main.go, and a provider.go…
official
terraform-test
hashicorp
Comprehensive guide for writing and running Terraform tests. Use when creating test files (.tftest.hcl), writing test scenarios with run blocks, validating…
official
terraform-test
hashicorp
Terraform परीक्षण लिखने और चलाने के लिए व्यापक मार्गदर्शिका, जिसमें अभिकथन, मॉकिंग और मॉड्यूल सत्यापन शामिल हैं। .tftest.hcl सिंटैक्स का उपयोग करके परीक्षण फ़ाइलें लिखें, जिनमें रन ब्लॉक हों जो प्लान या अप्लाई मोड में निष्पादित हों, अनुक्रमिक और समानांतर निष्पादन का समर्थन करते हुए वैकल्पिक स्थिति पृथक्करण के साथ। संसाधन विशेषताओं, आउटपुट और डेटा स्रोतों पर शर्तों का अभिकथन करें; अमान्य इ
official
provider-actions
hashicorp
प्लगइन फ्रेमवर्क का उपयोग करके संसाधन जीवनचक्र घटनाओं पर अनिवार्य Terraform प्रदाता क्रियाएँ लागू करें। बनाने से पहले/बाद और अपडेट करने से पहले/बाद जीवनचक्र ट्रिगर का समर्थन करता है (Terraform 1.14.0 में नष्ट करने की घटनाएँ उपलब्ध नहीं हैं)। सही फ्रेमवर्क प्रकार, संग्रह के लिए ElementType और इनपुट सत्यापन के लिए वैलिडेटर के साथ उचित स्कीमा परिभाषा की आवश्यकता है। लंबे समय तक चलने वाले संच
official
aws-ami-builder
hashicorp
Packer के amazon-ebs बिल्डर के साथ कस्टम Amazon Machine Images बनाएं। स्रोत AMI से HCL टेम्पलेट्स का उपयोग करके AMI निर्माण को स्वचालित करता है, जिसमें अनुकूलन के लिए प्रोविज़नर (शेल स्क्रिप्ट, फ़ाइल अपलोड, कॉन्फ़िगरेशन प्रबंधन) शामिल हैं। ami_regions के माध्यम से बहु-क्षेत्र AMI वितरण और नाम, स्वामी और वर्चुअलाइज़ेशन प्रकार के आधार पर लचीली स्रोत AMI फ़िल्टरिंग का समर्थन करता है। पर्यावरण चर, AWS क्रेडेंशियल फ़ाइल या
official
new-terraform-provider
hashicorp
Plugin Framework का उपयोग करके एक नया Terraform प्रदाता तैयार करें। मानक "terraform-provider-" नामकरण परंपरा के साथ एक नया Go मॉड्यूल वर्कस्पेस उत्पन्न करता है और आवश्यक निर्भरताओं को आरंभ करता है। HashiCorp के Plugin Framework पैटर्न का पालन करते हुए एक टेम्पलेट main.go फ़ाइल प्रदान करता है, जिसमें अनुकूलन के लिए TODO मार्कर होते हैं। बिल्ड और टेस्ट कमांड चलाकर सेटअप को मान्य करता है ताकि यह सुनिश्चित हो सके कि प्रदाता संकलित होता है और प्रारंभिक जांच पास कर
official
azure-verified-modules
hashicorp
Azure Terraform मॉड्यूल के लिए AVM अनुपालन हेतु प्रमाणन आवश्यकताएँ और सर्वोत्तम अभ्यास। प्रदाता संस्करण बाधाओं (azurerm >= 4.0, < 5.0; azapi >= 2.0, < 3.0) को लागू करता है और पिन किए गए Terraform रजिस्ट्री स्रोतों के पक्ष में git-आधारित मॉड्यूल संदर्भों को प्रतिबंधित करता है। सभी पहचानकर्ताओं के लिए लोअर स्नेक_केसिंग, सटीक चर प्रकार, एंटी-भ्रष्टाचार परत पैटर्न के माध्यम से अलग-अलग आउटपुट
official