0
votes

I'm writing an azure resource deployment project in Visual Studios 2015 to create the infrastructure for website which uses a mysql database living within a VM (also created in the same script) but I have yet to figure out how to create the VM from an existing image (vhd).

Ideally I'd like create a storage container, copy a template vhd to it and then create a VM using the copied template vhd. I know you can use the 'azcopy' to copy vhds across containers and storage accounts but I'm a bit hazy on doing all this in one resource deployment. Any ideas?

2

2 Answers

0
votes

There isn't a way to marshal the copying of the VHD during deployment, you'd need to do that beforehand (PowerShell, manually)... See this post for some more detail:

How to create vm using custom image from different storage account, on Azure

0
votes

The script snippet below gets all the variables needed to do a storage container copy (using azcopy.exe). To run this script just edit $sourceVhdUri and $sourceVhdKey (optionally you can change the storage account details too)

# Create destination storage account
$storageAccountName = "newstorageaccount"
$storageAccountType = "Standard_LRS"
$storageAccountContainerName = "vhds"
New-AzureStorageAccount -StorageAccountName $storageAccountName -ResourceGroupName $resourceGroupName -Type $storageAccountType -Location $location

# Set source details (found in Azure portal)
$sourceVhdUri = "URI TO STORAGE CONTAINER"
$sourceVhdKey = "STORAGE ACCOUNT ACCESS KEY"

# Get destination details
$destVhdUri = "https://$($storageAccountName).blob.core.windows.net/$($storageAccountContainerName)/"
$destVhdKey = (Get-AzureStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Key1

# local path to azcopy exe
$azCopyPath = "C:\Users\jordan\Documents\Visual Studio 2015\Projects\newVMfromImageTemplate\newVMfromImageTemplate\Tools\azcopy"

# run cmd command in powershell to copy container contents from source to destination container
& $azCopyPath $sourceVhdUri $destVhdUri /SourceKey:$sourceVhdKey /DestKey:$destVhdKey /S