2
votes

I am new to Packer and I have been trying to build my first packer on Azure image following this guide - https://docs.microsoft.com/en-us/azure/virtual-machines/windows/build-image-with-packer

I did not follow the guide exactly as I already had a an Azure Subscription and resource group set up. But now when I attempt to build my Packer image I get the following error

Build 'azure-arm' errored: Cannot locate the managed image resource group myResourceGroup

From reading through the docs it states the only requirement is that the resource group already exist, which it does as I can see it in my Azure portal.

My packer json file follows the one from the guide which is shown below, any help appreciated

    {
  "builders": [{
    "type": "azure-arm",

    "client_id": "0831b578-8ab6-40b9-a581-9a880a94aab1",
    "client_secret": "P@ssw0rd!",
    "tenant_id": "72f988bf-86f1-41af-91ab-2d7cd011db47",
    "subscription_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx",
    "object_id": "a7dfb070-0d5b-47ac-b9a5-cf214fff0ae2",

    "managed_image_resource_group_name": "myResourceGroup",
    "managed_image_name": "myPackerImage",

    "os_type": "Windows",
    "image_publisher": "MicrosoftWindowsServer",
    "image_offer": "WindowsServer",
    "image_sku": "2016-Datacenter",

    "communicator": "winrm",
    "winrm_use_ssl": "true",
    "winrm_insecure": "true",
    "winrm_timeout": "3m",
    "winrm_username": "packer",

    "azure_tags": {
        "dept": "Engineering",
        "task": "Image deployment"
    },

    "location": "East US",
    "vm_size": "Standard_DS2_v2"
  }],
  "provisioners": [{
    "type": "powershell",
    "inline": [
      "Add-WindowsFeature Web-Server",
      "if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}",
      "& $Env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /shutdown /quiet"
    ]
  }]
}
3
well, are you sure all the credentials are correct? does sp have access to the resource group?4c74356b41
Yes, I think so. $sp has been assigned as a contributor in azure with ' New-AzureRmRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $sp.ApplicationId'Ciaránimo
I know , this is an old post. I too have the same issue. First of all, I have a doubt that , where should we run this script on the client machine or remote machine ? Also , we are not specifying any source from where the Image has to be created?CrazyCoder

3 Answers

2
votes

There is an open issue with Packer where this error is displayed when there is a mismatch in the client_id / client_secret pair for details see here

2
votes

This happened to me because my service principal has no sufficient permissions to read the resource group.

Make sure you have the correct permission, this should create a correct service principal:

az ad sp create-for-rbac --role Contributor --name sp-packer-001
1
votes

Build 'azure-arm' errored: Cannot locate the managed image resource group myResourceGroup

According to this error message, it seems you have not replace your resource group name in that json file.

"managed_image_resource_group_name": "myResourceGroup",

We should replace that myResourceGroup with your existing resource group name, like this:

   "managed_image_resource_group_name": "jasonpacker",

Also we should replace client_id, client_secret, tenant_id, subscription_id and object_id.

After that complete, we can find the image in your existing resource group:

enter image description here