Skip to content

Commit

Permalink
Make CreateAzureVMFromPackerTemplate friendlier (actions#3209)
Browse files Browse the repository at this point in the history
* Don't assume JSON output

Specify JSON output, since it's required. The default can be changed.

* Make actions easier to track

To make cleanup easier, use the same GUID for all components and report at the end on what was created.
  • Loading branch information
howardjones authored May 5, 2021
1 parent f84d55a commit 0cca9be
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions helpers/CreateAzureVMFromPackerTemplate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,29 @@ Function CreateAzureVMFromPackerTemplate {
)

$vmSize = "Standard_DS2_v2"
$vnetName = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper()
$subnetName = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper()
$nicName = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper()
$publicIpName = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper()
$guid = [System.GUID]::NewGuid().ToString().ToUpper()
$vnetName = $env:UserName + "vnet-" + $guid
$subnetName = $env:UserName + "subnet-" + $guid
$nicName = $env:UserName + "nic-" + $guid
$publicIpName = $env:UserName + "pip-" + $guid

Write-Host "Creating a virtual network and subnet"
($vnet = az network vnet create -g $ResourceGroupName -l $AzureLocation -n $vnetName --address-prefixes 10.0.0.0/16 --subnet-name $subnetName --subnet-prefixes 10.0.1.0/24 --subscription $subscriptionId)
($vnet = az network vnet create -g $ResourceGroupName -l $AzureLocation -n $vnetName --address-prefixes 10.0.0.0/16 --subnet-name $subnetName --subnet-prefixes 10.0.1.0/24 --subscription $subscriptionId -o json)
$subnetId = ($vnet | ConvertFrom-Json).newVNet.subnets[0].id

Write-Host "`nCreating a network interface controller (NIC)"
($nic = az network nic create -g $ResourceGroupName -l $AzureLocation -n $nicName --subnet $subnetId --subscription $subscriptionId)
($nic = az network nic create -g $ResourceGroupName -l $AzureLocation -n $nicName --subnet $subnetId --subscription $subscriptionId -o json)
$networkId = ($nic | ConvertFrom-Json).NewNIC.id

Write-Host "`nCreating a public IP address"
($publicIp = az network public-ip create -g $ResourceGroupName -l $AzureLocation -n $publicIpName --allocation-method Static --sku Standard --version IPv4 --subscription $subscriptionId)
($publicIp = az network public-ip create -g $ResourceGroupName -l $AzureLocation -n $publicIpName --allocation-method Static --sku Standard --version IPv4 --subscription $subscriptionId -o json)
$publicIpId = ($publicIp | ConvertFrom-Json).publicIp.id

Write-Host "`nAdding the public IP to the NIC"
az network nic ip-config update -g $ResourceGroupName -n ipconfig1 --nic-name $nicName --public-ip-address $publicIpId --subscription $subscriptionId

Write-Host "`nCreating the VM"
az group deployment create -g $ResourceGroupName -n $VirtualMachineName --subscription $subscriptionId --template-file $templateFilePath --parameters vmSize=$vmSize vmName=$VirtualMachineName adminUserName=$AdminUsername adminPassword=$AdminPassword networkInterfaceId=$networkId

Write-Host "`nCreated in $(ResourceGroupName):`n vnet $(vnetName)`n subnet $(subnetName)`n nic $(nicName)`n publicip $(publicIpName)`n vm $(VirtualMachineName)"
}

0 comments on commit 0cca9be

Please sign in to comment.