{"id":1997,"date":"2025-06-10T03:09:03","date_gmt":"2025-06-10T03:09:03","guid":{"rendered":"https:\/\/www.nicktailor.com\/?p=1997"},"modified":"2025-07-11T03:23:35","modified_gmt":"2025-07-11T03:23:35","slug":"automate-rocky-linux-image-creation-in-azure-using-packer","status":"publish","type":"post","link":"https:\/\/nicktailor.com\/tech-blog\/automate-rocky-linux-image-creation-in-azure-using-packer\/","title":{"rendered":"Automate Rocky Linux Image Creation in Azure Using Packer"},"content":{"rendered":"<p>\u00a0<\/p>\n<p>Spinning up clean, custom Rocky Linux VMs in Azure doesn\u2019t have to involve manual configuration or portal clicks. With <strong>HashiCorp Packer<\/strong>, you can create, configure, and publish VM images to your Azure subscription automatically.<\/p>\n<h2>What You\u2019ll Need<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.packer.io\/\">Packer<\/a> installed<\/li>\n<li>Azure CLI (<code>az login<\/code>)<\/li>\n<li>Azure subscription &amp; resource group<\/li>\n<li>Azure Service Principal credentials<\/li>\n<\/ul>\n<h2>Step 1: Install Azure CLI<\/h2>\n<p>You need the Azure CLI to authenticate and manage resources.<\/p>\n<h3>On Linux\/macOS:<\/h3>\n<pre><code>curl -sL https:\/\/aka.ms\/InstallAzureCLIDeb | sudo bash<\/code><\/pre>\n<h3>On Windows:<\/h3>\n<p>Download and install from <a href=\"https:\/\/aka.ms\/installazurecli\">https:\/\/aka.ms\/installazurecli<\/a><\/p>\n<h2>Step 2: Login to Azure<\/h2>\n<pre><code>az login<\/code><\/pre>\n<p>This will open a browser window for you to authenticate your account.<\/p>\n<h2>Step 3: Set the Default Subscription (if you have more than one)<\/h2>\n<pre><code>az account set --subscription \"SUBSCRIPTION_NAME_OR_ID\"<\/code><\/pre>\n<h2>Step 4: Create a Resource Group for Images<\/h2>\n<pre><code>az group create --name packer-images --location eastus<\/code><\/pre>\n<h2>Step 5: Create a Service Principal for Packer<\/h2>\n<pre><code>az ad sp create-for-rbac \\\n  --role=\"Contributor\" \\\n  --scopes=\"\/subscriptions\/&lt;your-subscription-id&gt;\" \\\n  --name \"packer-service-principal\"<\/code><\/pre>\n<p>This will return the <code>client_id<\/code>, <code>client_secret<\/code>, <code>tenant_id<\/code>, and <code>subscription_id<\/code> needed for your variables file.<\/p>\n<h2>Step 6: Write the Packer Template (rocky-azure.pkr.hcl)<\/h2>\n<pre><code>variable \"client_id\" {}\nvariable \"client_secret\" {}\nvariable \"tenant_id\" {}\nvariable \"subscription_id\" {}\n\nsource \"azure-arm\" \"rocky\" {\n  client_id       = var.client_id\n  client_secret   = var.client_secret\n  tenant_id       = var.tenant_id\n  subscription_id = var.subscription_id\n\n  managed_image_resource_group_name = \"packer-images\"\n  managed_image_name                = \"rocky-image\"\n\n  os_type             = \"Linux\"\n  image_publisher     = \"OpenLogic\"\n  image_offer         = \"CentOS\"\n  image_sku           = \"8_2\"\n  location            = \"East US\"\n  vm_size             = \"Standard_B1s\"\n\n  capture_container_name    = \"images\"\n  capture_name_prefix       = \"rocky-linux\"\n  build_resource_group_name = \"packer-temp\"\n}\n\nbuild {\n  sources = [\"source.azure-arm.rocky\"]\n\n  provisioner \"shell\" {\n    inline = [\n      \"sudo dnf update -y\",\n      \"sudo dnf install epel-release -y\"\n    ]\n  }\n}<\/code><\/pre>\n<h2>Step 7: Create a Variables File (variables.pkrvars.hcl)<\/h2>\n<pre><code>client_id       = \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"\nclient_secret   = \"your-secret\"\ntenant_id       = \"your-tenant-id\"\nsubscription_id = \"your-subscription-id\"<\/code><\/pre>\n<h2>Step 8: Run the Build<\/h2>\n<pre><code>packer init .\npacker build -var-file=variables.pkrvars.hcl .<\/code><\/pre>\n<h2>Result<\/h2>\n<p>Your new custom Rocky Linux image will appear under your Azure resource group inside the <strong>Images<\/strong> section. From there, you can deploy it via the Azure Portal, CLI, Terraform, or ARM templates.<\/p>\n<p>This process makes your infrastructure repeatable, versioned, and cloud-native. Use it to standardize dev environments or bake in security hardening from the start.<\/p>\n<p>\u00a0<\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 Spinning up clean, custom Rocky Linux VMs in Azure doesn\u2019t have to involve manual configuration or portal clicks. With HashiCorp Packer, you can create, configure, and publish VM images to your Azure subscription automatically. What You\u2019ll Need Packer installed Azure CLI (az login) Azure subscription &amp; resource group Azure Service Principal credentials Step 1: Install Azure CLI You need<a href=\"https:\/\/nicktailor.com\/tech-blog\/automate-rocky-linux-image-creation-in-azure-using-packer\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[146,138],"tags":[],"class_list":["post-1997","post","type-post","status-publish","format-standard","hentry","category-azure","category-linux"],"_links":{"self":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/1997","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/comments?post=1997"}],"version-history":[{"count":4,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/1997\/revisions"}],"predecessor-version":[{"id":2011,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/1997\/revisions\/2011"}],"wp:attachment":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/media?parent=1997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/categories?post=1997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/tags?post=1997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}