How to create Azure VM with Public IP address through REST API - azure

how to I create a Azure VM with a public IP address using requests through there API. This is the current request body but I dont know what I need to add to have the VM have an public IP address - Thanks
{
"location": "ukwest",
"properties": {
"hardwareProfile": {
"vmSize": "Standard_B1s"
},
"storageProfile": {
"imageReference": {
"id": "/subscriptions/28f23ba2-c344-448c-808b-e45a97a29764/resourceGroups/main/providers/Microsoft.Compute/images/ukproxy"
},
"osDisk": {
"caching": "ReadWrite",
"managedDisk": {
"storageAccountType": "Standard_LRS"
},
"name": "myVMosdisk",
"createOption": "FromImage"
}
},
"osProfile": {
"adminUsername": "user",
"computerName": "user",
"adminPassword": "password!"
},
"networkProfile": {
"networkInterfaces": [
{
"id": "/subscriptions/28f23ba2-c344-448c-808b-e45a97a29764/resourceGroups/main/providers/Microsoft.Network/networkInterfaces/nic",
"properties": {
"primary": true
}
}
]
}
},
"name": "VM"
}

The public IP just associate to the network interface. So you do not need to worry about the public IP when you create the VM. All the thing you have to do is create the public IP and associate it to your interface which belongs to the VM.
For example, the request body when you create the network interface will like this:
{
"name": "test-nic",
"id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/networkInterfaces/test-nic",
"location": "eastus",
"properties": {
"provisioningState": "Succeeded",
"ipConfigurations": [
{
"name": "ipconfig1",
"id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/networkInterfaces/test-nic/ipConfigurations/ipconfig1",
"properties": {
"provisioningState": "Succeeded",
"privateIPAddress": "172.20.2.4",
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/publicIPAddresses/test-ip"
},
"subnet": {
"id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualNetworks/rg1-vnet/subnets/default"
},
"primary": true,
"privateIPAddressVersion": "IPv4"
}
}
],
"dnsSettings": {
"dnsServers": [],
"appliedDnsServers": []
},
"enableAcceleratedNetworking": true,
"enableIPForwarding": false
},
"type": "Microsoft.Network/networkInterfaces"
}

Related

How to get VM's private IP from the virtual machine scale set using the ARM template?

I created an ARM template with a virtual machine scale set, Now I am stuck in my ARM template development because I couldn't get private IPs of the virtual machine created inside the scale set. Private IPs are required to update Cassandra seeds using extension scripts.
{
"type": "Microsoft.Compute/virtualMachineScaleSets",
"apiVersion": "2020-06-01",
"name": "[parameters('vmName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[resourceId('Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
"Microsoft.Compute/images/myCustomImage"
],
"identity": {
"type": "SystemAssigned"
},
"sku": {
"name": "[parameters('vmSku')]",
"tier": "Standard",
"capacity": "[parameters('instanceCount')]"
},
"properties": {
"overprovision": "false",
"upgradePolicy": {
"mode": "Manual"
},
"virtualMachineProfile": {
"storageProfile": {
"imageReference": {
"id": "[resourceId('Microsoft.Compute/images', 'myCustomImage')]"
}
},
"osProfile": {
"computerNamePrefix": "[parameters('vmName')]",
"customdata": "1",
"adminUsername": "centos",
"linuxConfiguration": {
"disablePasswordAuthentication": true,
"ssh": {
"publicKeys": [
{
"path": "/home/centos/.ssh/authorized_keys",
"keyData": "xxxx"
}
]
}
}
},
"networkProfile": {
"networkInterfaceConfigurations": [
{
"name": "[parameters('nicName')]",
"properties": {
"primary": true,
"ipConfigurations": [
{
"name": "[variables('ipConfigName')]",
"properties": {
"subnet": {
"id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetworkName'), 'cassandra')]"
}
}
}
]
}
}
]
}
}
}
}
How to get private IPs of VMs created within the scale set?
You can use the function reference to get the NIC resource of the VMSS instances. And the resource id of the instance NICs, look like this:
/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmssName}/virtualMachines/{instanceId}/networkInterfaces/{nicName}
So you can set the variables for each instance id and then get the private IP addresses, the part of the template looks like this:
"variables": {
"instanceNic-0": "[concat('/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmssName}/virtualMachines/0/networkInterfaces/', parameters('nicName'))]"
},
"outputs": {
"instanceNic-0-IP": {
"type": "string",
"value": "[reference(variables('instanceNic-0'), '2016-09-01').ipConfigurations[0].properties.privateIPAddress]"
}
}

ARM- VMSS Output private ips of all instances

Im looking for a way to output all private ips of VMSS using outputs section in the ARM into an array. I have done a similar exercise to get public ip for a VM using the network interface ref [reference(variables('pipname')).ipAddress] and it worked, but here in VMSS since we do not have a NIC instance created, i'm not sure what object i need to reference. I looked online for documentation but could not find any. Please point me in the right direction. This is what i'm using
"variables": {
"appGatewayBackendPool": "appGatewayBackendPool",
"privateip": "/subscriptions/d6f9c1f8-f319-4a65-a590-86acc53e6e18/resourceGroups/maz-cac-iac-dal-gg-rg/providers/Microsoft.Compute/virtualMachineScaleSets/mazcaciacggapp/virtualMachines/networkInterfaces/mazcaciacggapp-nic"
},
"resources": [
{
"type": "Microsoft.Compute/virtualMachineScaleSets",
"name": "[concat(parameters('vmssName'))]",
"location": "[resourceGroup().location]",
"apiVersion": "2017-03-30",
"sku": {
"name": "[parameters('vmSku')]",
"capacity": "[parameters('instanceCount')]"
},
"properties": {
"overprovision": true,
"upgradePolicy": {
"mode": "Manual"
},
"virtualMachineProfile": {
"storageProfile": {
"osDisk": {
"createOption": "FromImage",
"caching": "[parameters('osDiskCaching')]",
"managedDisk": {
"storageAccountType": "[parameters('osDiskStorageType')]"
}
},
"dataDisks": [
{
"lun": 0,
"createOption": "Empty",
"caching": "[parameters('dataDiskCaching')]",
"diskSizeGB": "[parameters('vmssDataDisk1Size')]",
"managedDisk": {
"storageAccountType": "[parameters('dataDiskStorageType')]"
}
},
{
"lun": 1,
"createOption": "Empty",
"caching": "[parameters('dataDiskCaching')]",
"diskSizeGB": "[parameters('vmssDataDisk2Size')]",
"managedDisk": {
"storageAccountType": "[parameters('dataDiskStorageType')]"
}
}
],
"imageReference": {
"publisher": "[parameters('imageReference.publisher')]",
"offer": "[parameters('imageReference.offer')]",
"sku": "[parameters('imageReference.sku')]",
"version": "[parameters('imageReference.version')]"
}
},
"osProfile": {
"computerNamePrefix": "[parameters('vmssName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]"
},
"networkProfile": {
"networkInterfaceConfigurations": [
{
"name": "[concat(parameters('vmssName'), '-nic')]",
"properties": {
"primary": true,
"ipConfigurations": [
{
"name": "[concat(parameters('vmssName'), '-ipconfig')]",
"properties": {
"subnet": {
"id": "[resourceId(parameters('vnetResourceGroupName'),'Microsoft.Network/virtualNetworks/subnets', parameters('vnetName'), parameters('subnetName'))]"
},
"ApplicationGatewayBackendAddressPools": [
{
"id": "[concat('/subscriptions/', subscription().subscriptionId,'/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Network/applicationGateways/', parameters('appGatewayName'), '/backendAddressPools/', variables('appGatewayBackendPool'))]"
}
]
}
}
]
}
}
]
}
}
}
}
],
"outputs": {
"privateips": {
"type": "object",
"value": "[variables('privateip').IpConfigurations.PrivateIpAddress]",
}
}
}
resolved: used a powershell task in azure pipelines and set the ips as pipeline varibales

Azure Resource Management List Virtual Guests /providers/Microsoft.Compute/virtualMachines?api-version=2015-06-15

Below mentioned API is used to invoke all virtual machines :
https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Compute/virtualMachines?api-version=2015-06-15
In the response of virtual machines, virtual machine Id as mentioned here
"id": "/subscriptions/subscriptonId/resourceGroups/AGILITY/providers/Microsoft.Compute/virtualMachines/ProxyDontDelete10001",
in which resource group name is in Capital Letters (AGILITY) and if we invoke another rest api using this ID to get the instance view details it's not working.
/providers/Microsoft.Compute/virtualMachines/i-00000009/InstanceView
{
"value": [
{
"properties": {
"vmId": "7eb8dca3-dacf-4c51-b079-a508bf6d02b9",
"hardwareProfile": {
"vmSize": "Basic_A0"
},
"storageProfile": {
"osDisk": {
"osType": "Linux",
"name": "ProxyDontDelete10001",
"createOption": "FromImage",
"image": {
"uri": "https://blob.blob.core.windows.net/vhd/SM-RHEL6.7s-x64-9.2.r1664-20150801.vhd"
},
"vhd": {
"uri": "https://blob.blob.core.windows.net/vhds/ProxyDontDelete10001_ee751938-8d5c-468b-a36f-63e5332405cf.vhd"
},
"caching": "ReadWrite"
},
"dataDisks": [],
},
"osProfile": {
"computerName": "ProxyDontDelete10001",
"adminUsername": "admin",
"linuxConfiguration": {
"disablePasswordAuthentication": false
},
"secrets": [],
},
"networkProfile": {
"networkInterfaces": [
{
"id": "/subscriptions/{subscriptionId}/resourceGroups/test/providers/Microsoft.Network/networkInterfaces/testProxyDontDelete10001_ee751938-8d5c-468b-a36f-63e5332405cf"
}
],
},
"provisioningState": "Succeeded"
},
"type": "Microsoft.Compute/virtualMachines",
"location": "westus",
"id": "/subscriptions/subscriptonId/resourceGroups/AGILITY/providers/Microsoft.Compute/virtualMachines/ProxyDontDelete10001",
"name": "ProxyDontDelete10001"
},
Ok, I'm not sure I entirely understand the question, but resource group name is not case sensitive, so doing to:
/subscriptions/subscriptonId/resourceGroups/agility/providers/Microsoft.Compute/virtualMachines/ProxyDontDelete10001
should also work.

How to get Private IP of HDI cluster using ARM template

I have created template1 which will deploy HDI cluster and template2 which will deploy Azure VM seperately.
Now I want to get the Head-node Private IP from cluster and pass it to Azure VM template for processing using ARM template.
How can I do so?
Considering this is the object you are getting from HDcluster:
{
"id": "xxx",
"name": "xxx",
"type": "Microsoft.HDInsight/clusters",
"location": "East US",
"etag": "xxx",
"tags": null,
"properties": {
"clusterVersion": "3.5.1000.0",
"osType": "Linux",
"clusterDefinition": {
"blueprint": "https://blueprints.azurehdinsight.net/spark-3.5.1000.0.9865375.json",
"kind": "SPARK",
"componentVersion": {
"Spark": "1.6"
}
},
"computeProfile": {
"roles": [
{
"name": "headnode",
"targetInstanceCount": 2,
"hardwareProfile": {
"vmSize": "ExtraLarge"
},
"osProfile": {
"linuxOperatingSystemProfile": {
"username": "sshuser"
}
}
},
{
"name": "workernode",
"targetInstanceCount": 1,
"hardwareProfile": {
"vmSize": "Large"
},
"osProfile": {
"linuxOperatingSystemProfile": {
"username": "sshuser"
}
}
},
{
"name": "zookeepernode",
"targetInstanceCount": 3,
"hardwareProfile": {
"vmSize": "Medium"
},
"osProfile": {
"linuxOperatingSystemProfile": {
"username": "sshuser"
}
}
}
]
},
"provisioningState": "Succeeded",
"clusterState": "Running",
"createdDate": "2017-04-11T09:07:44.68",
"quotaInfo": {
"coresUsed": 20
},
"connectivityEndpoints": [
{
"name": "SSH",
"protocol": "TCP",
"location": "xxx.azurehdinsight.net",
"port": 22
},
{
"name": "HTTPS",
"protocol": "TCP",
"location": "xxx.azurehdinsight.net",
"port": 443
}
],
"tier": "standard"
}
}
I'm guessing this is the best output you can get, so you can use something like:
"outputs": {
"test": {
"type": "Object",
"value": "[reference(parameters('clusterName'),'2015-03-01-preview').connectivityEndpoints[0].location]"
}
}
This will get you an output of xxx.azurehdinsight.net
And you can either create a new deployment with this data or (just like I said) add RHEL VM to the same template and make it dependOn on the HDCluster deployment and reference the same thing as an input to VMextension.

output private ip of VM in azure ARM

I am spinning up more than one Azure VM using copyindex() in ARM template. Here is the resource I am using :
{
"apiVersion": "[variables('apiVersion')]",
"type": "Microsoft.Compute/virtualMachines",
"name": "[concat(parameters('vmDnsPrefixClientNode'),copyIndex(1))]",
"location": "[resourceGroup().location]",
"copy": {
"name": "virtualMachineLoop",
"count": "[parameters('vmInstancesClientNode')]"
},
"dependsOn": [
"[concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName'),copyindex(1))]",
"[concat('Microsoft.Network/networkInterfaces/', parameters('vmDnsPrefixClientNode'),copyindex(1),'-nic')]"
],
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmSizeClientNode')]"
},
"osProfile": {
"computername": "[concat(parameters('vmDnsPrefixClientNode'), copyIndex(1))]",
"adminUsername": "[parameters('username')]",
"adminPassword": "[parameters('password')]"
},
"storageProfile": {
"imageReference": {
"publisher": "[variables('imagePublisher')]",
"offer": "[variables('imageOffer')]",
"sku": "[variables('ubuntuOSVersion')]",
"version": "latest"
},
"osDisk": {
"name": "osdisk1",
"vhd": {
"uri": "[concat('http://',variables('storageAccountName'),copyindex(1),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',parameters('vmDnsPrefixClientNode'),copyIndex(1),'-osdisk1.vhd')]"
},
"caching": "ReadWrite",
"createOption": "FromImage"
},
"dataDisks": [
{
"name": "datadisk1",
"diskSizeGB": "10",
"lun": 0,
"vhd": {
"uri": "[concat(reference(concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName'),copyindex(1)), variables('apiVersion')).primaryEndpoints.blob, variables('vmDataContainerName'),'/',parameters('vmDnsPrefixClientNode'),copyIndex(1),'-',variables('dataDisk1VhdName'),'.vhd')]"
},
"createOption": "Empty"
}
]
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces',concat(parameters('vmDnsPrefixClientNode'),copyindex(1),'-nic'))]"
}
]
}
}
},
I tried something like this, which isn't working
"outputs": {
"privateIP": {
"value": "[reference(concat(parameters('vmDnsPrefixClientNode'),copyindex(1),'-nic'),providers('Microsoft.Network', 'privateIPAddresses').apiVersions[0]).dnsSettings.fqdn]",
"type": "string",
"copy": {
"name": "vmNic",
"count": "[parameters('vmInstancesClientNode')]"
}
}
}
anyone knows how to get private IP or internal FQDN in output ?
I have used the following code in my template to get the private ip address from network interface.
"outputs":{
"networkInterface":{
"value": "[reference(resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName')),'2016-09-01')]",
"type": "object"
}
}
Once you get the output, then you can find the IP address at
outputs.networkInterface.value.ipConfigurations[0].properties.privateIPAddress
and dns suffix at
outputs.networkInterface.value.dnsSettings.internalDomainNameSuffix

Resources