Azure resource group is a container that holds all the
related resources for an application. The resource group could include all of
the resources for an application, or only those resources that are logically
grouped together. With the proper use of Azure resource manager you can now
decide how you want to allocate resources to resource groups based on what
makes the most sense for your organization.
Azure resource manager supports specifying the template in
JSON format that defines the deployment and configuration of your machines/
application. This provides you the ease of repeatedly deploying your
infrastructure in a consistent state. It also makes it easy for defining your
infrastructure as a configuration instead of coding it. Dependencies are also handled
easily and in an ordered way compared to the coded approach of defining the
infrastructure.
In this post we’ll see how to use Azure PowerShell to create
and manage a resource group and add a virtual machine to the group. Please note
that you need to have PowerShell version 3.0 or above and Azure PowerShell
version 0.9.0 available to use the resource manager cmdlets.
You can check the PowerShell version by typing
$PSVersionTable in PowerShell console
PS C:\> $PSVersionTable
Name
Value
----
-----
PSVersion
5.0.10105.0
WSManStackVersion
3.0
SerializationVersion
1.1.0.1
CLRVersion
4.0.30319.0
BuildVersion
10.0.10105.0
PSCompatibleVersions
{1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion
2.3
To check the Azure PowerShell version use the cmdlet
Get-Module Azure | select Version on the console
PS C:\> Get-Module Azure | select version
Version
-------
0.9.3
Before starting to create the resource group, we need to add
the Azure account to PowerShell session using the Add-AzureAccount cmdlet
Add-AzureAccount
Note: If you have multiple subscriptions available you need
to select a subscription using the Select-AzureSubscription cmdlet.
Azure PowerShell installation includes more than one
PowerShell module. To use the Azure resource manager module, we must explicitly
switch to the resource manager module by using the Switch-AzureMode cmdlet as
given below.
Switch-AzureMode AzureResourceManager
Next step is to create the JSON file to use as a template.
For our sample I;ve used the same one from Microsoft Azure website as given
below.
{
"$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountName": {
"type": "string",
"metadata":
{
"Description":
"Unique DNS Name for the Storage Account
where the Virtual Machine's disks will be placed."
}
},
"username": {
"type":
"string",
"metadata": {
"Description":
"Username for the Virtual Machine."
}
},
"password": {
"type":
"securestring",
"metadata":
{
"Description":
"Password for the Virtual Machine."
}
},
"dnsName": {
"type":
"string",
"metadata":
{
"Description":
"Unique DNS Name for the Public IP used
to access the Virtual Machine."
}
},
"osVersion":
{
"type":
"string",
"defaultValue":
"2012-R2-Datacenter",
"allowedValues":
[
"2008-R2-SP1",
"2012-Datacenter",
"2012-R2-Datacenter",
"Windows-Server-Technical-Preview"
],
"metadata":
{
"Description":
"The Windows version for the VM. This
will pick a fully patched image of this given Windows version. Allowed values:
2008-R2-SP1, 2012-Datacenter, 2012-R2-Datacenter,
Windows-Server-Technical-Preview."
}
}
},
"variables": {
"location": "West
Europe",
"imagePublisher": "MicrosoftWindowsServer",
"imageOffer": "WindowsServer",
"OSDiskName": "osdiskforwindowssimple",
"nicName": "armDemoVM01",
"addressPrefix": "10.0.0.0/16",
"subnetName": "Subnet",
"subnetPrefix": "10.0.0.0/24",
"storageAccountType": "Standard_LRS",
"publicIPAddressName": "armDemoPublicIP01",
"publicIPAddressType": "Dynamic",
"vmStorageAccountContainerName": "vhds",
"vmName": "ARMDemoVM01",
"vmSize": "Standard_D1",
"virtualNetworkName": "ARMDEMOVNET01",
"vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
"subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]"
},
"resources": [
{
"type":
"Microsoft.Storage/storageAccounts",
"name":
"[parameters('storageAccountName')]",
"apiVersion":
"2015-05-01-preview",
"location":
"[variables('location')]",
"properties":
{
"accountType":
"[variables('storageAccountType')]"
}
},
{
"apiVersion":
"2015-05-01-preview",
"type":
"Microsoft.Network/publicIPAddresses",
"name":
"[variables('publicIPAddressName')]",
"location":
"[variables('location')]",
"properties":
{
"publicIPAllocationMethod": "[variables('publicIPAddressType')]",
"dnsSettings":
{
"domainNameLabel": "[parameters('dnsName')]"
}
}
},
{
"apiVersion":
"2015-05-01-preview",
"type":
"Microsoft.Network/virtualNetworks",
"name":
"[variables('virtualNetworkName')]",
"location":
"[variables('location')]",
"properties":
{
"addressSpace":
{
"addressPrefixes": [
"[variables('addressPrefix')]"
]
},
"subnets":
[
{
"name": "[variables('subnetName')]",
"properties":
{
"addressPrefix":
"[variables('subnetPrefix')]"
}
}
]
}
},
{
"apiVersion":
"2015-05-01-preview",
"type":
"Microsoft.Network/networkInterfaces",
"name":
"[variables('nicName')]",
"location":
"[variables('location')]",
"dependsOn":
[
"[concat('Microsoft.Network/publicIPAddresses/',
variables('publicIPAddressName'))]",
"[concat('Microsoft.Network/virtualNetworks/',
variables('virtualNetworkName'))]"
],
"properties":
{
"ipConfigurations": [
{
"name":
"ipconfig1",
"properties":
{
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id":
"[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
},
"subnet":
{
"id":
"[variables('subnetRef')]"
}
}
}
]
}
},
{
"apiVersion":
"2015-05-01-preview",
"type":
"Microsoft.Compute/virtualMachines",
"name":
"[variables('vmName')]",
"location":
"[variables('location')]",
"dependsOn":
[
"[concat('Microsoft.Storage/storageAccounts/',
parameters('storageAccountName'))]",
"[concat('Microsoft.Network/networkInterfaces/',
variables('nicName'))]"
],
"properties":
{
"hardwareProfile": {
"vmSize":
"[variables('vmSize')]"
},
"osProfile":
{
"computername":
"[variables('vmName')]",
"adminUsername":
"[parameters('username')]",
"adminPassword":
"[parameters('password')]"
},
"storageProfile": {
"imageReference": {
"publisher":
"[variables('imagePublisher')]",
"offer":
"[variables('imageOffer')]",
"sku":
"[parameters('osVersion')]",
"version":
"latest"
},
"osDisk":
{
"name":
"osdisk",
"vhd":
{
"uri":
"[concat('http://',parameters('storageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('OSDiskName'),'.vhd')]"
},
"caching":
"ReadWrite",
"createOption":
"FromImage"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id":
"[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
}
]
}
}
}
]
}
Save this file to a JSON file on the disk. In this sample
I’ve saved the file as armdemo01.json.
Next we have to create a Azure resource group using the
New-AzureResourceGroup cmdlet as given below.
$location="West Europe"
$resourceGroupName = "ARMRGDemo01"
New-AzureResourceGroup -Name $resourceGroupName
-Location $location
PS C:\PSScripts> New-AzureResourceGroup -Name
$resourceGroupName -Location $location
ResourceGroupName : ARMRGDemo01
Location :
westeurope
ProvisioningState : Succeeded
Tags :
Permissions :
Actions NotActions
======= ==========
*
ResourceId :
/subscriptions/2a9f4b1c-91fd-4fe6-8f9a-fd2ea8aa4840/resourceGroups/ARMRGDemo01
By opening the management portal we can now verify the
resource group created successfully.
After creating the resource group, we can now use the
template to manage the resources in the resource group. To create the
deployment group, run the New-AzureResourceGroupDeployment cmdlet as given
below.
$resourceGroupName = "ARMRGDemo01"
$templateFile = "C:\PSScripts\armdemo01.json"
New-AzureResourceGroupDeployment -Name "ARMDemoDeployment01"
-ResourceGroupName $resourceGroupName
-TemplateFile $templateFile
PS C:\PSScripts> New-AzureResourceGroupDeployment -Name
"ARMDemoDeployment01" -ResourceGroupName $resourceGroupName
-TemplateFile $templateFile
cmdlet New-AzureResourceGroupDeployment at command pipeline
position 1
Supply values for the following parameters:
(Type !? for Help.)
storageAccountNameFromTemplate: armstrgdemo01
username: prajeesh
dnsName: armcsdemo01
DeploymentName :
ARMDemoDeployment01
ResourceGroupName : ARMRGDemo01
ProvisioningState : Succeeded
Timestamp :
7/27/2015 5:32:04 PM
Mode :
Incremental
TemplateLink :
Parameters :
Name Type Value
=============== ========================= ==========
storageAccountName String armstrgdemo01
username String prajeesh
password SecureString
dnsName String armcsdemo01
osVersion String 2012-R2-Datacenter
Outputs
:
During the execution, the command will prompt for some
parameters as given in the example above, provide values for the storage
account name, virtual machine username and password to proceed.
After successful execution, we can now verify the details on
the management portal to see the resources created in the group.