Hashi Packer

arindam0310018

Arindam Mitra

Posted on May 1, 2024

Hashi Packer

Greetings my fellow Technology Advocates and Specialists.

This is Chapter #1 of my Packer Series.

In this Session, I will demonstrate how to Automate image builds with Packer in Azure.

I had the Privilege to talk on this topic in ONE Azure Communities:-

NAME OF THE AZURE COMMUNITY TYPE OF SPEAKER SESSION
Cloud Lunch and Learn - 2024 Virtual
EVENT ANNOUNCEMENTS:-
Image description
CODE REPOSITORY:-



POINTS TO NOTE:-
1. Cloud Provider is Microsoft Azure.
2. For the purpose this blog post, we are building image for Windows using Packer.
PRE-REQUISITES:-
1. Azure Subscription.
2. Azure Resource Group.
3. Azure Service Principal - This will be used by Packer to Authenticate.
4. Azure Service Principal having "Contributor" RBAC on Subscription or on the specific Resource Group where Packer will create Image.
5. Download and Install Packer.
Image description
Image description
Image description
UNDERSTAND PACKER TERMINOLOGY:-
Browse to the following link - https://developer.hashicorp.com/packer/docs/terminology
Image description
UNDERSTAND BUILDERS:-
Builders create machines and generate images from those machines for various platforms.
Browse to the following link - https://developer.hashicorp.com/packer/docs/builders
Image description
The type of builder, we will be using is Plugin.
Click on "Plugin" (Refer the above screenshot) and it will redirect you to the following link - https://developer.hashicorp.com/packer/integrations
Image description
Click on "Azure vX.X.X" (Refer the above screenshot) and it will redirect you to the following link - https://developer.hashicorp.com/packer/integrations/hashicorp/azure
The Azure ARM builder supports building Virtual Hard Disks (VHDs) and Managed Images in Azure Resource Manager.
Image description
PACKER TEMPLATE (am-packer-template-v1.pkr.hcl):-
This template builds a Windows Server 2019 VM, installs IIS, then generalizes the VM with Sysprep.
The IIS install shows how you can use the PowerShell provisioner to run additional commands.
The final Packer image then includes the required software install and configuration.


source "azure-arm" "am-image" {
  subscription_id                   = "210e66cb-55cf-424e-8daa-6cad804ab604"
  tenant_id                         = "20516b3d-42af-4bd4-b2e6-e6b4051af72a"
  client_id                         = "54b7f78d-6b11-466c-8172-5934f104e779"
  client_secret                     = "xxxxxxxxxxxxxxxxxxxxxxx"
  managed_image_name                = "am-image-v1"
  managed_image_resource_group_name = "am-packer-rg"
  communicator                      = "winrm"
  image_offer                       = "WindowsServer"
  image_publisher                   = "MicrosoftWindowsServer"
  image_sku                         = "2019-Datacenter"
  location                          = "westeurope"
  os_type                           = "Windows"
  vm_size                           = "Standard_B4ms"
  winrm_insecure                    = "true"
  winrm_timeout                     = "5m"
  winrm_use_ssl                     = "true"
  winrm_username                    = "packeradmin"
}

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

  provisioner "powershell" {
    inline = ["Add-WindowsFeature Web-Server", "while ((Get-Service RdAgent).Status -ne 'Running') { Start-Sleep -s 5 }", "while ((Get-Service WindowsAzureGuestAgent).Status -ne 'Running') { Start-Sleep -s 5 }", "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit", "while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10  } else { break } }"]
  }

}



Enter fullscreen mode Exit fullscreen mode
PACKER COMMANDS:-
Below follows the list of Packer commands.
Note:- "C:\Packer" Contains the Packer executable and Packer HCL Template.
1. Initialize Packer:-

.\packer.exe init C:\Packer\.

2. Format Packer Template:-

.\packer.exe fmt C:\Packer\.

3. Validate Packer:-

.\packer.exe validate C:\Packer\.

4. Build Packer:-

.\packer.exe build C:\Packer\am-packer-template-v1.pkr.hcl

BELOW FOLLOWS ALL THE TROUBLESHOOTING STEPS:-
ERROR #1:-
Unknown source type "azure-rm". The source "azure-rm" is unknown by Packer, and is most likely part of a plugin that is not installed.
Image description
RESOLUTION:-
The Plugin was installed.
.\packer.exe plugins install github.com/hashicorp/azure
Image description
PACKER IMAGE BUILD SUCCESFULLY COMPLETED:-
1. Windows Image build execution with Packer completed successfully.
Image description
2. During the time, Packer build runs, an actual Virtual machine with all required resources gets created in a temporary resource group. The Windows VM Image gets created from that Virtual machine and its related resources. Once the Image is created successfully, the virtual machine and all its related resources gets deleted.
Image description
3. Finally, the Windows Image created using Packer.
Image description

Hope You Enjoyed the Session!!!

Stay Safe | Keep Learning | Spread Knowledge

💖 💪 🙅 🚩
arindam0310018
Arindam Mitra

Posted on May 1, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Hashi Packer
azure Hashi Packer

May 1, 2024