1

I have problem deploying more than one azure vms from custom image (aka ami) built using packer.

Here's my packer script to create the base image:

{
  "builders": [
    {
      "type": "azure-arm",
      "client_id": "CHANGE_ME",
      "client_secret": "CHANGE_ME",
      "object_id": "CHANGE_ME",
      "subscription_id": "CHANGE_ME",
      "tenant_id": "CHANGE_ME",
      "resource_group_name": "packerrgvm",
      "storage_account": "packerrgvm",
      "capture_container_name": "images",
      "capture_name_prefix": "packer",
      "os_type": "Linux",
      "image_publisher": "Canonical",
      "image_offer": "UbuntuServer",
      "image_sku": "16.04.0-LTS",
      "azure_tags": {
        "dept": "engineering"
      },
      "location": "westeurope",
      "vm_size": "Standard_A2"
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "inline": ["do sth interesting here"]
    },
    {
      "type": "shell",
      "inline": [
        "sudo /usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync"
      ]
    }
  ]
}

Now, I'm trying to deploy new vm using ARM templates. My template contains imageName, imageUri and vhdUri provided by packer after successfull build. Vnets, network interfaces etc ommited:

{
  "apiVersion": "2016-03-30",
  "type": "Microsoft.Compute/virtualMachines",
  "name": "[variables('workerVM').machine.name]",
  "location": "[resourceGroup().location]",
  "properties": {
    "hardwareProfile": {
      "vmSize": "[variables('workerVM').machine.size]"
    },
    "storageProfile": {
      "osDisk": {
        "osType": "Linux",
        "name": "[variables('workerVM').machine.imageName]",
        "createOption": "FromImage",
        "image": {
          "uri": "[variables('workerVM').machine.imageUri]"
        },
        "vhd": {
          "uri": "[variables('workerVM').machine.vhdUri]"
        },
        "caching": "ReadWrite"
      }
    },
    "osProfile": {
      "computerName": "[variables('workerVM').machine.name]",
      "adminUsername": "[variables('workerVM').machine.adminUsername]",
      "adminPassword": "[variables('workerVM').machine.adminPassword]"
    },
    "networkProfile": {
      "networkInterfaces": [
        {
          "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('workerVM').network.nicName)]"
        }
      ]
    },
    "diagnosticsProfile": {
      "bootDiagnostics": {
        "enabled": false
      }
    },
    "provisioningState": 0
  }
}

When I deploy it for the first time, it works. Hovewer, even if I remove my resource group completely and try to deploy the vm once again, I get the following error:

error:   The resource operation completed with terminal provisioning state 'Failed'.
error:   Blob https://packerrgvm.blob.core.windows.net/vmcontainera1ba96d3-a593-44b9-8c71-1d345ef67a2d/osDisk.a1ba96d3-a593-44b9-8c71-1d345ef67a2d.vhd already exists. Please provide a different blob URI as target for disk 'packer-osDisk.49359f62-5c49-44c1-aed8-4ea1613ab2e9.vhd'.

Is it possible to use custom ami built by packer this way?

slnowak
  • 1,839
  • 3
  • 23
  • 37
  • Please double check whether the resource 'osDisk.a1ba96d3-a593-44b9-8c71-1d345ef67a2d.vhd' was exist or not before deploying this VM? Maybe this resource haven't been deleted successfully before you re-deploy the VM. – Amor May 15 '17 at 09:10
  • Yeah, surely it wasn't deleted. My question is more like: 'how do i create more than 1 vm from single base image built with backer'? – slnowak May 15 '17 at 09:51

2 Answers2

0

how do i create more than 1 vm from single base image built with backer

You could change the value vhdUri variable before re-deploy the ARM template.

"vhd": {
    "uri": "[variables('workerVM').machine.vhdUri]"
}

The vhdUri variable in your ARM template could be like this,

"variables": {
  "workerVM": {
    "machine": {
      "vhdUri": "reset this uri"
    }
 }
Amor
  • 8,325
  • 2
  • 19
  • 21
  • The vhdUri could be like this, https://[YourAccountName].blob.core.windows.net/[YourContainerName]/[YourDiskName].vhd. Please also make sure that the account name is exist in your subscription and the container and blob are already created in this storage account. If not, please create them before redeploy the ARM template. – Amor May 17 '17 at 08:44
0

In ARM template add below storage resource which uses image uri generated by packer

{
  "type": "Microsoft.Compute/images",
  "apiVersion": "2016-04-30-preview",
  "name": "[variables('imageName')]",
  "location": "[resourceGroup().location]",
  "properties": {
    "storageProfile": {
      "osDisk": {
        "osType": "Windows",
        "osState": "Generalized",
        "blobUri": "[parameters('your_image_uri_generated_by_packer')]",
        "storageAccountType": "Standard_LRS"
      }
    }
  }
},

And modify storage profile property in Microsoft.Compute/virtualMachines to use this resource

"storageProfile": {
  "imageReference": {
        "id": "[resourceId('Microsoft.Compute/images', variables('imageName'))]"
      }
},
apurva
  • 16
  • 2